Angular aot / prod 模式下的延迟加载
Angular Lazy Loading in aot / prod mode
im 使用 Angular 6 并且 Angular 中有两种延迟加载模块的方法:
第一种方法:将模块路径指定为字符串
{path: 'debug', loadChildren: 'app/global/debug.module/debug.module#DebugModule'},
方法二:指定模块为函数签名
{path: 'login', loadChildren: () => LoginMockModule},
在开发模式下 buth 方法有效,但在生产模式下第二种方法给我以下错误:
ERROR Error: Uncaught (in promise): Error: Runtime compiler is not loaded Error: Runtime compiler is not loaded
有人可以解释为什么第二种方法在生产模式下不起作用。我想使用这种模式,因为
- 它不太容易出错,因为 IDE 可以检查模块是否正确导入
- 重构/移动模块省事了,因为IDE知道导入的模块已经移动了。
感谢您的帮助
这是 angular-cli Github () 上反复出现的问题。问题出在AOT的使用上,因为prod模式默认使用AOT。您可以找到 angular-cli 开发人员之一给出的简短解释:
Angular CLI only supports lazy route detection via the loadChildren string syntax, while the repro uses a function instead. Since the lazy route is not detected and compiled via AOT, that error comes up.
所以...唯一剩下的就是希望它会在下一个版本中得到修复。同时,唯一的方法是使用 loadChildren
.
im 使用 Angular 6 并且 Angular 中有两种延迟加载模块的方法:
第一种方法:将模块路径指定为字符串
{path: 'debug', loadChildren: 'app/global/debug.module/debug.module#DebugModule'},
方法二:指定模块为函数签名
{path: 'login', loadChildren: () => LoginMockModule},
在开发模式下 buth 方法有效,但在生产模式下第二种方法给我以下错误:
ERROR Error: Uncaught (in promise): Error: Runtime compiler is not loaded Error: Runtime compiler is not loaded
有人可以解释为什么第二种方法在生产模式下不起作用。我想使用这种模式,因为
- 它不太容易出错,因为 IDE 可以检查模块是否正确导入
- 重构/移动模块省事了,因为IDE知道导入的模块已经移动了。
感谢您的帮助
这是 angular-cli Github (
Angular CLI only supports lazy route detection via the loadChildren string syntax, while the repro uses a function instead. Since the lazy route is not detected and compiled via AOT, that error comes up.
所以...唯一剩下的就是希望它会在下一个版本中得到修复。同时,唯一的方法是使用 loadChildren
.