首页 > 解决方案 > Angular 7 路由 concat 导致编译错误

问题描述

首先 - 仅供参考,路由模块设置为:

@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

这里已经提出了几个类似的问题,但没有解决这个问题。有没有可能的解决方案?可能是对正在发生的事情的解释?

标签: angulartypescriptconcat

解决方案


对于那些正在寻找有答案的帖子的人——正如丹尼尔上面所建议的那样,这似乎是可行的。如果有任何潜在问题,请发表评论。

export const ALL_ROUTES: Routes =
    [
      ...ROUTES1,
      ...ROUTES2
    ];

推荐阅读