2016-10-04 93 views
0

我有一个小方法,使用Compiler('@angular/core')compileModuleAndAllComponentsAsync()方法创建一个组件。当我创建组件,具有下列错误没有提供当compileModuleAndAllComponentsAsync()angular2 - 2.0最终版本

无提供者名称服务“

我知道这是什么错误,但我尝试编译模块有这个提供商:!

@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(); 
    }); 
}); 

回答

0

经过数小时寻找代码,我发现问题是因为Component没有provider:[NameService]。我插入了提供程序,并都正常工作。

我花了很长时间才发现这个问题,因为组件工作正常,只有当我尝试动态创建它时,就是问题发生了。

相关问题