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
服務時,它只有在我沒有將它指定爲數組時纔有效......但它是一個數組。
通過打字稿,這個屬性被標識爲一個對象,我不能重複它...