2017-04-06 53 views
0

我的孩子路线的作品很好,当创建它们喜欢:角2块为孩子的路线不createds如果生孩子的阵列befor路线对象

let routes: Routes = [ 
    { 
     path: '', 
     component: MainComponent, 
     children: [ 
      {path: 'a', loadChildren: '../+a/a.module.ts#AModule', canLoad: [AGuard]}, 
      {path: 'b', loadChildren: '../+b/b.module.ts#BModule', canLoad: [BGuard]} 
     ] 
    } 
]; 

,并为所有模块创建块,当我尝试之前初始化儿童这样,所有的停止工作,并且我不吨有块这个模块:

let children: Routes = []; 

children.push({path: 'a', loadChildren: '../+a/a.module.ts#AModule', canLoad: [AGuard]}) 
children.push({path: 'b', loadChildren: '../+b/b.module.ts#BModule', canLoad: [BGuard]}) 

let routes: Routes = [ 
    { 
     path: '', 
     component: MainComponent, 
     children: children 
    } 
]; 

而这很奇怪,我认为这是相同的代码。

回答

0

作为结果我创建2环境文件第一environment.a.ts具有路线的

export const environment = { 
    .... 
    {path: 'a', loadChildren: '../+a/a.module.ts#AModule', canLoad: [AGuard]} 
} 

和第二environment.b.ts具有途径b

export const environment = { 
    .... 
    {path: 'b', loadChildren: '../+b/b.module.ts#BModule', canLoad: [BGuard]} 
} 

,我后可以用我的路线,如:

import {environment} from "../../environments/environment"; 
let routes: Routes = [ 
    { 
     path: '', 
     component: MainComponent, 
     children: environment.routes 
    } 
]; 

和块正常创造

0

尝试将儿童定义为通用数组,而不是键入Router。

let children:any = []; 

children.push({path: 'a', loadChildren: '../+a/a.module.ts#AModule', canLoad: [AGuard]}) 
children.push({path: 'b', loadChildren: '../+b/b.module.ts#BModule', canLoad: [BGuard]}) 

let routes: Routes = [ 
    { 
     path: '', 
     component: MainComponent, 
     children: children 
    } 
]; 
+0

我tring,但它仍然不工作 –