Javascript 康卡特角2路

Javascript 康卡特角2路,javascript,angular,routes,Javascript,Angular,Routes,有没有办法在Angular 2中连接两个routes数组?我需要将路由定义拆分为不同的文件,以便有多个要合并的变量。 我尝试过使用数组推送,但没有成功 i、 e(这是有效的): 但这不起作用(当我导航到/page1时,出现“找不到第1页模块”错误) 我做错什么了吗?只需创建一个新变量 function getRoutes(){ // Let's make this an array let customRoutes: Routes = [{ path: 'page1',

有没有办法在Angular 2中连接两个routes数组?我需要将路由定义拆分为不同的文件,以便有多个要合并的变量。 我尝试过使用数组推送,但没有成功

i、 e(这是有效的):

但这不起作用(当我导航到/page1时,出现“找不到第1页模块”错误)


我做错什么了吗?

只需创建一个新变量

function getRoutes(){
  // Let's make this an array
  let customRoutes: Routes = [{
    path: 'page1',
    loadChildren: './page1/page1.module#Page1Module'
  }]
  var allRoutes: Routes = []; // Push all routes into this
  routes.forEach(r => allRoutes.push(r));
  // Even better would be to do this with a condition in the forEach instead of hardcoding the index
  allRoutes[1].children.push(customRoutes);
  // allRoutes.push(otherCustomRoutes);
  return allRoutes;
};

@NgModule({
  imports: [ RouterModule.forRoot(getRoutes()) ],
  exports: [ RouterModule ]
})

我使用的是Angular 8,所以可能这在过去不起作用?

只需创建一个新变量

function getRoutes(){
  // Let's make this an array
  let customRoutes: Routes = [{
    path: 'page1',
    loadChildren: './page1/page1.module#Page1Module'
  }]
  var allRoutes: Routes = []; // Push all routes into this
  routes.forEach(r => allRoutes.push(r));
  // Even better would be to do this with a condition in the forEach instead of hardcoding the index
  allRoutes[1].children.push(customRoutes);
  // allRoutes.push(otherCustomRoutes);
  return allRoutes;
};

@NgModule({
  imports: [ RouterModule.forRoot(getRoutes()) ],
  exports: [ RouterModule ]
})

我使用的是Angular 8,所以可能这在过去不起作用?

请澄清。第一个没有合并的简单示例是否不起作用?您没有在
RouterModule.forRoot(routes)
中使用函数
getRoutes()
。是的,对不起,应该是
RouterModule.forRoot(getRoutes())
,但我无法编辑我的答案<代码>函数getRoutes()也应该是
导出函数getRoutes()
。第一个例子有效,第二个不起作用。你是否设法解决了这个问题?我也需要确定我的路线,请澄清。第一个没有合并的简单示例是否不起作用?您没有在
RouterModule.forRoot(routes)
中使用函数
getRoutes()
。是的,对不起,应该是
RouterModule.forRoot(getRoutes())
,但我无法编辑我的答案<代码>函数getRoutes()也应该是
导出函数getRoutes()
。第一个例子有效,第二个不起作用。你是否设法解决了这个问题?我也需要检查我的路线。
function getRoutes(){
  // Let's make this an array
  let customRoutes: Routes = [{
    path: 'page1',
    loadChildren: './page1/page1.module#Page1Module'
  }]
  var allRoutes: Routes = []; // Push all routes into this
  routes.forEach(r => allRoutes.push(r));
  // Even better would be to do this with a condition in the forEach instead of hardcoding the index
  allRoutes[1].children.push(customRoutes);
  // allRoutes.push(otherCustomRoutes);
  return allRoutes;
};

@NgModule({
  imports: [ RouterModule.forRoot(getRoutes()) ],
  exports: [ RouterModule ]
})