【发布时间】:2019-09-03 00:05:52
【问题描述】:
我正在尝试从库中延迟加载 NgModule。我有一个 ng 应用程序,其中包含一些库(项目)。这个库在其他一些项目中被重用。问题是我找不到适用于 jit 和 aot 以及已编译/未编译库的解决方案。
文件结构是这样的
app
-projects
--lib
---(lib files)
-src
--(app files)
AppModule 有路由,长这样
const routes: Routes = [
{
path: 'eager',
children: [
{
path: '',
component: LibComponent // component imported from lib
},
{
path: 'lazy',
loadChildren: 'lib/src/lib/lazy/lazy.module#LazyModule' // module i want to load lazily
}
]
}
];
如果我这样使用它,当我尝试导航到 jit 中的惰性路由时会出现运行时错误(aot 工作正常):
ERROR Error: Uncaught (in promise): TypeError: undefined is not a function TypeError: undefined is not a function
此评论 https://github.com/angular/angular-cli/issues/9488#issuecomment-370065452 建议不要将 LazyModule 包含到任何桶文件中,但如果我将其从库的 public_api 中排除,则会出现构建错误:
ERROR in ./projects/lib/src/lib/lazy/lazy.module.ts
Module build failed (from ./node_modules/@ngtools/webpack/src/index.js):
Error: C:\projects\lazy_minimal\lazy-minimal\projects\lib\src\lib\lazy\lazy.module.ts is missing from the TypeSc
ript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
at AngularCompilerPlugin.getCompiledFile (C:\projects\lazy_minimal\lazy-minimal\node_modules\@ngtools\webpac
k\src\angular_compiler_plugin.js:752:23)
at plugin.done.then (C:\projects\lazy_minimal\lazy-minimal\node_modules\@ngtools\webpack\src\loader.js:41:31
)
at process._tickCallback (internal/process/next_tick.js:68:7)
有什么办法让它同时适用于 aot 和 jit 吗?
【问题讨论】:
-
没有包装模块仍然是不可能的:github.com/angular/angular-cli/issues/…
-
你有没有尽量不使用桶文件?过去我在使用桶文件和延迟加载模块时发生过奇怪的行为。
-
使用包装器的技巧似乎只延迟加载包装器模块,当我需要延迟的库中的模块包含在主包中时
-
桶文件是库根目录下的公共api,它被指定为ng-package.json的入口点。我如何构建库来避免它?
-
查看更新后的帖子@ADAmelin
标签: angular angular-cli lazy-loading jit