繁体   English   中英

当compileModuleAndAllComponentsAsync()angular2-2.0最终版本时没有提供者

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM