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屋!
查看全文