首页 > 解决方案 > Angular:在根模块中加载 JIT 编译的动态模块

问题描述

我生成了一个 JIT 编译模块,但无法将它加载到根模块中。任何帮助将不胜感激..这是我解决这个问题的第三天

JIT 编译模块 - dynaModule

let dynaModule = NgModule({
        declarations: dyna_comps,
        exports: dyna_comps,
        imports: [CommonModule, RouterModule.forChild([
            {
                path: 'cc1',
                component: dyna_comps[0]
            },
            {
                path: 'cc2',
                component: dyna_comps[1]
            }
        ]),
        ],
        providers: [GlobalService], //- e.g.if your dynamic component needs any service, provide it here.

    })(class { });

this.compiler.compileModuleAndAllComponentsAsync(dynaModule)
        .then((factories) => {
            for (let i = 0; i < factories.componentFactories.length; i++)
            {
                const f = factories.componentFactories[i];
                this.cmpRef = f.create(this.injector, [], null, this.moduleRef);
                this.cmpRef.instance.name = 'DCOMP' + i;                  
            }    
     });

对于静态模块,我可以使用 router.resetConfig 方法延迟加载它

this.router.resetConfig([
        ...this.router.config,
        {
            path: 'c',
            loadChildren: '../static/static.module#StaticModule'               

        }
    ]);

如何在根模块中加载这个 JIT compile dynaModule 以便完成路由?

标签: angularrouting

解决方案


推荐阅读