有人可以告诉我是否可以在新视图中打开子路由,而不是直接在它下面打开?如果是这样的话,如何以及如何回到父亲以前的航线导航状态?
而不是
const routes: Routes = [
{ path: 'first', component: ExampleComponent, children: [
{ path: 'second', component: Example2Compoennt, }
] }
];
Do:
const routes: Routes = [
{ path: 'first', component: ExampleComponent },
{ path: 'first/second', component: Example2Compoennt },
];
使用children
很好,因为如果您想使用一些guard
,它将适用于所有子路由,当您创建经过身份验证的路由时,它非常有用。
示例:
const routes: Routes = [
{
path: 'first',
children: [
{ path: '', component: FirstComponent },
{ path: 'second', component: SecondComponent },
{
path: 'third',
canActivate: [YourGuard],
children: [
{ path: '', component: ThirdComponent },
{ path: 'another-route', component: FourthComponent },
]
}
],
}
];