【发布时间】:2018-12-06 14:27:45
【问题描述】:
我在调试简单的 Angular 应用程序时遇到了问题。每当我尝试将服务注入构造函数时,我都会收到一条错误消息:无法解析 LoginService 的所有参数(?)
app.component.ts
import { Component, Injector } from '@angular/core';
import { LoginService } from './login.service';
@Component({
selector: 'app-home',
template: require('./app.component.html')
})
export default class AppComponent {
constructor(private loginService: LoginService) {}
}
login.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class LoginService {
constructor(private http: HttpClient) {
}
login() {
// Do http requests here
}
}
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule, HttpClient } from '@angular/common/http';
// Components
import AppComponent from './app.component';
//Services
import { LoginService } from './login.service';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, HttpClientModule],
providers: [LoginService, HttpClient],
bootstrap: [AppComponent]
})
export default class AppModule {}
似乎是 HttpClient 注入的问题,但我无法弄清楚到底是什么。
你有什么想法吗?
【问题讨论】:
-
可能是 impott 的路径是错误的,或者可能拼写错误,就像在实际大写 L 中一样,但你拼写小 l
-
我很确定你的
HttpClient中不需要app.module.ts,它应该只在服务中导入 -
@Und3rTow,你说得对,谢谢,删除,我快绝望了!
标签: javascript angular angular5