我在 app.module 中的提供者定义如下:
{
multi: true,
deps: [ServiceA, ServiceB],
provide: HTTP_INTERCEPTORS,
useClass: HttpResponseInterceptor,
}
它工作正常。问题是,我需要 ServiceB 可以通过工厂功能进行配置,并且 serviceB 是可选的,但是这样的东西不起作用:
// Factory function
serviceBFactory = (param) => { // returns instance of my service or null, depends on param }
...
deps: [ ServiceA, { provide: ServiceB, useFactory: serviceBFactory } ]
...
我得到的错误:
Error: StaticInjectorError(AppModule)[InjectionToken HTTP_INTERCEPTORS -> [object Object]]:
StaticInjectorError(Platform: core)[InjectionToken HTTP_INTERCEPTORS -> [object Object]]
NullInjectorError: No provider for [object Object]!
是否有任何特殊语法或设计不可能?