Angular 2,如果为路径对象创建子数组,则不会创建子路径块

时间:2017-04-06 13:21:49

标签: angular lazy-loading angular2-routing

我的儿童路线在创建它们时效果很好:

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
    }
];

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

2 个答案:

答案 0 :(得分:0)

尝试将define children作为通用数组,而不是键入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
    }
];

答案 1 :(得分:0)

结果我创建了2个环境文件1st environment.a.ts有路径a

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
    }
];

和chunk正常创造者