首先 - 仅供引用,路由模块设置为:
@NgModule({
imports: [RouterModule.forRoot(ALL_ROUTES)],
exports: [RouterModule],
providers: []
})
export class AppRoutingModule {}
将所有路由放在名为“ALL_ROUTES”的 1 个数组中,并将该数组传递给 AppRoutingModule - 导入:[RouterModule.forRoot(ALL_ROUTES)],工作正常:
export const ALL_ROUTES: Routes = [
{path: 'route1', component: FirstComponent},
{path: 'route2', component: SecondComponent},
{path: 'route3', component: ThirdComponent},
{path: 'route4', component: FourthComponent}
];
所以,上面的代码工作正常。 但是,如果我们有 2 个数组并像这样连接它们:
export const ROUTES1: Routes = [
{path: 'route1', component: FirstComponent},
{path: 'route2', component: SecondComponent}
];
export const ROUTES2: Routes = [
{path: 'route3', component: ThirdComponent},
{path: 'route4', component: FourthComponent}
];
export const ALL_ROUTES: Routes = ROUTES1.concat(ROUTES2);
我们得到一个编译错误:
ERROR in Cannot read property 'loadChildren' of undefined
这里问了几个类似的问题,但没有解决这个问题。有可能的解决方案吗?可能是对正在发生的事情的解释?
最佳答案
对于那些正在搜索有答案的帖子的人 - 正如 Danil 上面所建议的那样,这个接缝很有用。如果有任何潜在问题,请发表评论。
export const ALL_ROUTES: Routes =
[
...ROUTES1,
...ROUTES2
];
关于Angular 7路由连接导致编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54195192/