Angular 9使用动态路线动态导入模块 [英] Angular 9 Dynamically import modules with dynamic routes
本文介绍了Angular 9使用动态路线动态导入模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用后端服务中的可变字符串路由动态导入角度模块. 例如,我的后端服务在应用程序启动时(使用APP_INITIALIZER)向我发送此响应.
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",
},
},
},
我的应用程序结构为:
所以我想导入这样的模块
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);
但是我遇到了这个错误,我认为webpack无法处理这样的动态负载.
But i'm getting this error, i think webpack does not handle dynamic loads like this.
谢谢您的帮助:)
推荐答案
不幸的是,使用动态字符串进行动态导入是不可能的
Unfortunately, using dynamic string for dynamic imports is impossible
这篇关于Angular 9使用动态路线动态导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文