Angular 9 Dynamically import modules with dynamic routes

China☆狼群 提交于 2020-06-26 12:15:25

问题


I want to import angular modules dynamically with variable string routes from backend service. For example my backend service sends to me this response when app is starting(using APP_INITIALIZER).

{
    "hostname": "a-tenant",
    "modules": {
        "home": {
            "class": "HomeAModule",
            "path": "home-a.module",
         },
    },
},

My app structure is:

So i want to import a module like this

const path = `./tenants/${response.hostname}/home/${response.modules.home.path}`;

import(path).then(m => m[response.modules.home.class]);

My final import code should be like this in runtime:

import('./tenants/a-tenant/home/home-a.module').then(m => m.HomeAModule);

But i'm getting this error, i think webpack does not handle dynamic loads like this.

Thank you for your help :)


回答1:


Unfortunately, using dynamic string for dynamic imports is impossible



来源:https://stackoverflow.com/questions/62011462/angular-9-dynamically-import-modules-with-dynamic-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!