【发布时间】:2016-11-04 02:38:16
【问题描述】:
我实现了 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 问题/答案,但找不到类似的示例..
谢谢!
【问题讨论】: