我有以下代码动态加载模块:
export class DynamicQuestionComponent {
constructor(
private componentFactoryResolver: ComponentFactoryResolver,
private viewContainerRef: ViewContainerRef
) {}
@Input() form: FormBase;
@Input() formgroup: FormGroup;
@ViewChild('content', { read: ViewContainerRef, static: true }) content: ViewContainerRef;
@Input() set question(qvalue){
if (qvalue){
this.content.clear();
var compath = `./${qvalue.qType}-question.component`;
var testpath = './proto-question.component';
import('./proto-question.component').then( dyncomp => {
const component = this.viewContainerRef.createComponent(this.componentFactoryResolver.resolveComponentFactory(dyncomp.ProtoQuestionComponent));
(<any>component).instance.form = this.form;
(<any>component).instance.question = qvalue;
(<any>component).instance.formgroup = this.formgroup;
this.content.insert(component.hostView);
component.injector.get(ChangeDetectorRef).markForCheck();
})
}
}
}目前,这是它的唯一工作方式,如果我硬编码导入函数内部的组件路径。我希望能够将一个变量传递到导入函数中,但是每次切换到变量时,我都会得到可怕的无法找到模块错误的信息:
ERROR Error: Uncaught (in promise): Error: Cannot find module './proto-question.component'正如您所看到的,我甚至测试了一个与硬编码版本完全相同的变量,这个变量也失败了。
我觉得一定要有个合适的环境才能让这件事正常运作。
我正在使用:
角9.1.9
角CLI 9.1.4
假设我可以通过动态变量问题,那么我需要弄清楚如何将动态组件传递到resolveComponentFactory调用中。
发布于 2020-06-11 13:09:19
不幸的是,按变量加载动态模块是不可能的。我也有类似的问题,我找不到解决办法。Dynamically import modules with dynamic routes
https://stackoverflow.com/questions/62304831
复制相似问题