如何在Angular 2中设置嵌套路由?

时间:2016-08-18 07:32:52

标签: angular routing

我在Angular2中有一些路线

{ path: 'items', component: ItemComponent }

ItemComponent有自己的子菜单。

<a routerLink='/items/sales'>Sales</a>
<a routerLink='/items/suppliers'>Suppliers</a>
<a routerLink='/items/stock'>Stock</a>

我希望子菜单在所有这些组件(销售,供应商,库存)中可见

我正在尝试在ItemComponent中放置第二个路由器插座,然后应显示销售/供应商/库存。原因是我希望子菜单在所有这些上都可见。

如何设置这样的嵌套路线?

或者我应该只创建一个ItemMenuComponent并在销售,供应商和库存组件的顶部放置一个元素?

1 个答案:

答案 0 :(得分:4)

您可以定义这样的儿童路线。

[    
    {
        path: 'items',
        component: ItemsComponent,
        children: [
            {
                path: '', // default
                component: SubItemComponent1
            },
            {
                path: 'item1',
                component: SubItemComponent1
            },
            {
                path: 'item2',
                component: SubItemComponent1
            }
        ]
    },
    ....
    ....
]

更多信息:https://github.com/narainsagar/angular2-demos/blob/master/src/app/app.routes.ts#L17

干杯,