[英]No provider when compileModuleAndAllComponentsAsync() angular2 - 2.0 final version
我有一個使用Compiler
類('@angular/core')
和compileModuleAndAllComponentsAsync()
方法創建組件的小方法。 當我創建組件時,出現以下錯誤
沒有NameService提供商!”。
我知道這是什么錯誤,但是我嘗試編譯的模塊具有此提供程序:
@NgModule({
imports: [CommonModule, RouterModule, FormsModule],
declarations: [MyComponent],
providers: [NameService]
})
export class MyModule {}
因此,如何在創建的組件上注入提供程序NameService
和其他提供程序?
this.compiler.compileModuleAndAllComponentsAsync(this._createDynamicComponent())
.then(factory => {
const compFactory = factory.componentFactories.find((x: any) => x.componentType === this.instanceComponent());
const cmpRef = this.vcRef.createComponent(compFactory);
this.addToInstance(cmpRef, (cmpRef) => {
cmpRef.destroy();
});
});
經過數小時的尋找代碼,我發現問題是因為該Component
沒有provider:[NameService]
。 我插入了提供程序,並且一切正常。
我花了很長時間才發現這一點,因為該組件正常工作,只有當我嘗試動態創建它時,問題才發生了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.