Angular 在运行时编译和创建组件
我正在尝试用angular制作一个文档生成工具,我遇到了一个挑战,即如何允许用户动态创建内容 我想要创建的组件可能具有任意的模型和行为,因此我认为我不能使用共享组件 我描述的组件在编译时不存在 我看到一些。但是,它提到您必须在Angular 在运行时编译和创建组件,angular,dynamic,compile-time,Angular,Dynamic,Compile Time,我正在尝试用angular制作一个文档生成工具,我遇到了一个挑战,即如何允许用户动态创建内容 我想要创建的组件可能具有任意的模型和行为,因此我认为我不能使用共享组件 我描述的组件在编译时不存在 我看到一些。但是,它提到您必须在ngModule的entryComponents中列出“动态”组件。在我的场景中,将不起作用 有没有其他机制可以达到这种效果 您可以动态创建一个模块和一个组件,对其应用decorators,然后对其进行编译。然后,您将能够访问已编译的组件: @ViewChild('vc',
ngModule
的entryComponents
中列出“动态”组件。在我的场景中,将不起作用
有没有其他机制可以达到这种效果
您可以动态创建一个模块和一个组件,对其应用decorators,然后对其进行编译。然后,您将能够访问已编译的组件:
@ViewChild('vc', {read: ViewContainerRef}) vc: ViewContainerRef;
constructor(private _compiler: Compiler,
private _injector: Injector,
private _m: NgModuleRef<any>) {
}
ngAfterViewInit() {
const template = '<span>generated on the fly: {{name}}</span>';
const tmpCmp = Component({template: template})(class {
});
const tmpModule = NgModule({declarations: [tmpCmp]})(class {
});
this._compiler.compileModuleAndAllComponentsAsync(tmpModule)
.then((factories) => {
const f = factories.componentFactories[0];
const cmpRef = f.create(this._injector, [], null, this._m);
cmpRef.instance.name = 'dynamic';
this.vc.insert(cmpRef.hostView);
})
}
@ViewChild('vc',{read:ViewContainerRef})vc:ViewContainerRef;
构造函数(私有编译器:编译器,
专用注射器:注射器,
私有:NgModuleRef
您好。谢谢您提供的信息。您知道angular 8/ivy将如何更改吗?我相信您现在可以将AOT用于核心,并将JIT用于生成的代码fly@user5328504,现在还不清楚。我们需要等一下,直到Ivy在Angular8中被释放。我确实收到了错误消息在代码中调用compileModuleAndAllComponentsAsync时出现“错误:运行时编译器未加载”。有什么线索吗?@MaxKoretskyi,现在Ivy在这里,能否就@user5328504的问题给出一个方向?我很乐意@MaxKoretskyi