2
我实现了http拦截器服务,但它工作正常,但我的问题是我无法弄清楚如何在提供声明中注入我自己的服务。Angular 2:无法在提供程序中使用useFactory注入服务
我在控制台中得到运行时错误: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问题/答案,但couldn” t找到一个类似的例子..
谢谢!
我已经更新了我的问题,所以你可以调整,我使用{提供:“为MyService”,useClass:为MyService},在我的供应商阵列,但只是为MyService只有(如你所建议的)工作! –