无法读取 null 的属性“loadChildren"中的 Angular CLI 错误 [英] Angular CLI ERROR in Cannot read property 'loadChildren' of null
问题描述
我正在将一个项目转换为使用 angular cli 并且一切正常(一旦构建),但在构建过程中我有一个奇怪的行为.
I'm converting a project to use angular cli and everything is working (once it's build) but i've got a weird behaviour during build.
使用 ng serve
我第一次尝试时总是收到此错误:无法读取 null 的属性loadChildren"中的错误
with ng serve
I always get this error the first time I try: ERROR in Cannot read property 'loadChildren' of null
使用 ng build
抛出同样的错误
with ng build
throws the same error
但是如果我使用 ng build --watch
在第一次构建失败并且我编辑一个文件以再次触发构建后,它将成功.我也有与 ng build -prod --watch
but if I use ng build --watch
and after the first build fails and I edit a file to trigger the build again it will succeed. I've got also the same behaviour with ng build -prod --watch
任何想法如何在第一次正确构建?
Any ideas how to get the build right the first time?
请注意,我的项目中没有任何子路由/模块,而且我没有任何其他输出来查看是什么导致了这种情况.
更新:使用子路由测试,我仍然得到相同的行为
Update: tested with child routes and I still get the same behaviour
更新:将@angular libs 从 4.0.0 降级到 2.4.0,我仍然得到完全相同的行为,但有不同的错误消息;
Update: downgraded @angular libs to 2.4.0 from 4.0.0 and I still get the exact same behaviour, but with a different error message;
AppModule 中的错误不是 NgModule
ERROR in AppModule is not an NgModule
推荐答案
我的路由有多个 ts 文件,我所做的是在 app.routes 中导入所有这些路由,然后将它们连接在一起以调用 forRoot:
I had multiple ts files for my routes and what I did was import all those routes in my app.routes and then concat them all together to call forRoot:
export const appRoutes = xRoutes.concat(zRoutes)
etc
将我的所有路由放入一个文件/export const 语句后,错误/奇怪的构建行为消失了.
after putting all my routes in one single file/export const statement the error/weird build behaviour disappeared.
这篇关于无法读取 null 的属性“loadChildren"中的 Angular CLI 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!