首页 > 解决方案 > Angular 2+父/子路由

问题描述

我设置了这两条路线

{ path: 'manage-brands', component: ManageBrandsComponent, canActivate: [AuthGuard] },

{ path: 'manage-brand/:brandId',  component: BrandAdminComponent },

但我真正想要的是:/manage-brands -> ManageBrandsComponent 然后在路由 /manage-brands/brandId -> BrandAdminComponent 中是否应该有一个 id

有没有办法使用 children: [ ] 来实现这一点,还是我必须只使用一个组件来呈现基于 RouteSnapshot 的 ng 模板?

标签: angularangular2-routing

解决方案


这是实现此行为的父子方式。

const routes: Routes = [

{
    path: 'manage-brands', component: ManageBrandsComponent,
    canActivate: [AuthGuard],
    children: [
      { path: ':brandId', component: BrandAdminComponent }
    ]
}

]

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})

export class AppRoutingModule { }


推荐阅读