首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何以角度9按变量加载动态模块?

如何以角度9按变量加载动态模块?
EN

Stack Overflow用户
提问于 2020-06-10 13:29:46
回答 1查看 671关注 0票数 2

我有以下代码动态加载模块:

代码语言:javascript
复制
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();
            })
        }
    }
}

目前,这是它的唯一工作方式,如果我硬编码导入函数内部的组件路径。我希望能够将一个变量传递到导入函数中,但是每次切换到变量时,我都会得到可怕的无法找到模块错误的信息:

代码语言:javascript
复制
ERROR Error: Uncaught (in promise): Error: Cannot find module './proto-question.component'

正如您所看到的,我甚至测试了一个与硬编码版本完全相同的变量,这个变量也失败了。

我觉得一定要有个合适的环境才能让这件事正常运作。

我正在使用:

角9.1.9

角CLI 9.1.4

假设我可以通过动态变量问题,那么我需要弄清楚如何将动态组件传递到resolveComponentFactory调用中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-11 13:09:19

不幸的是,按变量加载动态模块是不可能的。我也有类似的问题,我找不到解决办法。Dynamically import modules with dynamic routes

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62304831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档