Angular 路由和子路由管理

Angular route and child route management

有人能告诉我是否可以在新视图中打开子路由而不是直接在它下面打开? 如果是,如何以及如何回到父路由之前的导航状态?

而不是

const routes: Routes = [
   { path: 'first', component: ExampleComponent, children: [
       { path: 'second', component: Example2Compoennt, }
   ] }
];

做:

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