简体   繁体   English

Angular:从配置文件延迟加载模块

[英]Angular: Lazy loading modules from config file

In my project there is a config.json in which different modules are defined that should be loaded.在我的项目中有一个 config.json,其中定义了应该加载的不同模块。

So I want to load them dynamically and lazy.所以我想动态地和懒惰地加载它们。

const MODULE_PATH: string = './local-features/local-test-feature/local-test-feature.module';
const MODULENAME: string = 'LocalTestFeatureModule';
const ROUTE_PATH: string = 'local-test-feature';


lazyRoutes.push({
 path: ROUTE_PATH,
 loadChildren: () => import(MODULE_PATH).then((u) => (<any>u)[MODULENAME]),
});

However, angular throws exceptions if the import method is not passed a hardcoded string as a parameter but a variable.但是,如果导入方法未将硬编码字符串作为参数而是变量传递,angular 将抛出异常。

core.mjs:9229 ERROR Error: Uncaught (in promise): Error: Cannot find module './local-features/local-test-feature/local-test-feature.module'
Error: Cannot find module './local-features/local-test-feature/local-test-feature.module'
    at  lazy namespace object:5:1
    at _ZoneDelegate.invoke (zone.js:375:26)
    at Object.onInvoke (core.mjs:26597:33)
    at _ZoneDelegate.invoke (zone.js:374:52)
    at Zone.run (zone.js:134:43)
    at zone.js:1278:36
    at _ZoneDelegate.invokeTask (zone.js:409:31)
    at core.mjs:26276:55
    at AsyncStackTaggingZoneSpec.onInvokeTask (core.mjs:26276:36)
    at _ZoneDelegate.invokeTask (zone.js:408:60)
    at resolvePromise (zone.js:1214:31)
    at resolvePromise (zone.js:1168:17)
    at zone.js:1281:17
    at _ZoneDelegate.invokeTask (zone.js:409:31)
    at core.mjs:26276:55
    at AsyncStackTaggingZoneSpec.onInvokeTask (core.mjs:26276:36)
    at _ZoneDelegate.invokeTask (zone.js:408:60)
    at Object.onInvokeTask (core.mjs:26584:33)
    at _ZoneDelegate.invokeTask (zone.js:408:60)
    at Zone.runTask (zone.js:178:47)

How can I solve this?我该如何解决这个问题?

you can't realy use constants inside of dynamic imports.您不能真正在动态导入中使用常量。 webpack should know what exactly you are trying to import to build modules tree correctly. webpack 应该知道您要导入什么以正确构建模块树。

If I got the problem right: you want to put module loading logic somewhere to the module itself and only import that small entryfile with minimal config from that module, and when user navigates to the path - import the whole module.如果我做对了问题:您想将模块加载逻辑放在模块本身的某个地方,并且只从该模块导入带有最小配置的小入口文件,当用户导航到路径时 - 导入整个模块。

everyting is possible unless import('path/to/module') piece broken.除非import('path/to/module')损坏,否则一切皆有可能。 for example例如

file: module-entry.ts

export const route: Route = {
  path: 'local-test-feature',
  loadChildren: () => import('./local-features/local-test-feature/local-test-feature.module').then(m => m.LocalTestFeatureModule),
  // this is even more typesafe from ts point of view as it can resolve what is goint to be imported on static level
};

usage:

import {route as localTestFeatureRoute} from './path-to/module-entry.ts'


const routes: Route[] = [
  anyOtherRoute,
  localTestFeatureRoute,
  ...etc
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM