【发布时间】:2018-12-26 12:35:15
【问题描述】:
我正在使用 Angular 编译器的 compileModuleAndAllComponentsAsync 在 Angular v6 中创建动态组件,下面的代码相同。
viewChangeMethod(view: string) {
let template = `<span>${view} </span>`;
const tmpCmp = Component({ template: template })(class { });
this._compiler.clearCacheFor(this.tmpModule)
this.tmpModule = NgModule({ declarations: [tmpCmp,FRAComponent],import:[ComonModule] })(class {
});
this._compiler.compileModuleAndAllComponentsAsync(this.tmpModule)
.then((factories) => {
const f = factories.componentFactories[0];
const cmpRef = f.create(this._injector, [], null, this._m);
this._container.detach()
console.log(cmpRef.hostView)
this._container.insert(cmpRef.hostView);
})
this._compiler.clearCacheFor(this.tmpModule)
}
一切正常,但在导入任何共享模块或自定义模块时出现错误。
【问题讨论】:
-
我看不到您在哪里或如何尝试导入模块并使用它们 - 您可以包含该代码吗?您是否添加了
imports[]并将所需的模块添加到this.tmpModule = NgModule({ declarations: [tmpCmp] })(class {这一行中? -
是的,我在 tmpModule 中导入组件和模块
标签: angular angular-components angular-compiler angular-dynamic-components