我实现了http拦截器服务,但是我的问题是我无法弄清楚如何在提供声明中注入我自己的服务。
我在控制台中遇到运行时错误:No provider for MyService
。
HttpService拦截器类:
// all the imports...
@Injectable()
export class HttpService extends Http {
constructor(backend: ConnectionBackend,
defaultOptions: RequestOptions,
private router: Router,
private injector: ReflectiveInjector,
private myService: MyService){
super(backend, defaultOptions);
}
// methods...
在我的app.module.ts中,我有一系列提供者:
[
{
provide: 'MyService',
useClass: MyService
},
{
provide: 'Http',
useFactory: (xhrBackend: XHRBackend,
requestOptions: RequestOptions,
router: Router,
injector: ReflectiveInjector,
myService: MyService) =>
new HttpService(xhrBackend, requestOptions, router, injector, myService),
deps: [XHRBackend, RequestOptions, Router, Injector, MyService]
}]
我已经检查了几个SO问题/答案,但无法找到类似的例子..
谢谢!
答案 0 :(得分:4)
deps
仅说出工厂需要的依赖关系(以便可以正确注入)。但它实际上并没有提供依赖性。您仍然需要将其添加到providers
数组
providers: [
MyService,
{
provide: 'Http',
...
}
]