0
我在我的应用程序使用多个供应商时的一个问题:角多服务供应商阵列
ERROR Error: No provider for Array!
at injectionError (VM634 core.umd.js:1238) [angular]
at noProviderError (VM634 core.umd.js:1276) [angular]
at ReflectiveInjector_._throwOrNull (VM634 core.umd.js:2777) [angular]
at ReflectiveInjector_._getByKeyDefault (VM634 core.umd.js:2816) [angular]
at ReflectiveInjector_._getByKey (VM634 core.umd.js:2748) [angular]
at ReflectiveInjector_.get (VM634 core.umd.js:2617) [angular]
at AppModuleInjector.NgModuleInjector.get (VM634 core.umd.js:3585) [angular]
at resolveDep (VM634 core.umd.js:11046) [angular]
at createClass (VM634 core.umd.js:10899) [angular]
at createDirectiveInstance (VM634 core.umd.js:10730) [angular]
at createViewNodes (VM634 core.umd.js:12093) [angular]
at createRootView (VM634 core.umd.js:11998) [angular]
at callWithDebugContext (VM634 core.umd.js:13213) [angular]
at Object.debugCreateRootView [as createRootView] (VM634 core.umd.js:12673) [angular]
我的代码
@Injectable()
abstract class OtherService<O> {
protected parentProp: O;
constructor() {
}
}
@Injectable()
class OtherServiceImpl extends OtherService<any> {
private prop;
constructor() {
super();
}
}
@NgModule({
})
class OtherModule {
static forRoot(): OtherModule {
return {
ngModule: OtherModule,
providers: [
{provide: OtherService, useFactory:() => new OtherServiceImpl(), multi: true},
{provide: OtherService, useFactory:() => new OtherServiceImpl(), multi: true}
],
};
}
}
@Component({
selector: 'app-root',
template: `
<pre>{{services | json}}</pre>
`
})
class AppComponent {
// IF I USE (public services: OtherService<any>) INSTEAD, IT WORKS, IT'S AND ARRAY BUT NOT USABLE AS AN ARRAY TYPE IN MY COMPONENT
constructor(public services: OtherService<any>[]) {
}
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
OtherModule.forRoot()
],
bootstrap: [AppComponent]
})
class AppModule {
}
你可以找到一个工作示例这个运动员:https://plnkr.co/edit/Nui2eFwS3CtT1fYKDpzh?p=preview
正如你所看到的,在AppComponent中,当我注入我的multi
服务时,它只有在我没有将它指定为数组时才有效......但它是一个数组。
通过打字稿,这个属性被标识为一个对象,我不能重复它...