@Injectable()
export class MyService {
constructor(private http: Http, @Inject('name') @Optional() public name?: string) {
}
在appModule里面我正在尝试使用提供程序进行服务
MyService,
imports: [ // import Angular's modules
HttpModule,
....,
[
{ provide: 'username', useValue: 'default' }
],
const injector = ReflectiveInjector.resolveAndCreate(
[MyService,
{
provide: 'name', useValue: 'abc'
}
]);
但我得到了
错误错误:没有Http提供商! (MyService - > Http)
答案 0 :(得分:0)
在你的app.module.ts中导入两个:
import { HttpClientModule } from '@angular/common/http';
import { HttpModule } from '@angular/http';
然后将它们添加到您的导入中:
imports: [
...
HttpClientModule,
HttpModule
],
请务必将您的服务添加到提供商:
providers: [
...
MyService
...
],
然后将以下内容添加到您的服务中:
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
将它添加到构造函数中,引用HttpClient而不是Http,如下所示:
constructor(private http: HttpClient)
希望它有所帮助。