[英]Angular 6 routing - setting children route component based on parent route parameters
我的Angular 6項目中具有以下路由配置。
path: 'chart/:feature',
component: ChartStateComponent,
canActivateChild: [ChartGuardService],
children: [
{
path: ':id',
redirectTo: '/chart/:feature/:id/profile',
pathMatch: 'full'
},
{ path: ':id/:tab',
component: TestOneComponent,
pathMatch: 'full'
}
]
feature
是PARAM到父路徑,它可以像4個不同的值one
, two
, three
和four
。 現在,根據此參數,我需要為children
路徑設置組件。
例如,如果feature
設置為one
,則將TestOneComponent
設置為子組件。
我怎樣才能做到這一點?
您可以嘗試以下方法:
path: 'chart',
component: ChartStateComponent,
canActivateChild: [ChartGuardService],
children: [
{
path: 'one/:id',
redirectTo: 'one/:id/profile',
pathMatch: 'full'
},
{ path: 'one/:id/:tab',
component: TestOneComponent
},
{ path: 'two/:id/:tab',
component: TestTwoComponent
},
...
],
或者,您可以構建路由防護器以處理更復雜的路由。 有關更多信息, 請參見此問題: Angular2條件路由
UPDATE
我不確定如何基於參數添加條件...但是可以基於全局變量添加條件。
在“全局”文件中:
export class Globals {
public static ISADMIN: boolean = false;
}
在您的模塊路由定義中
{
path: 'products/:id',
component: Globals.ISADMIN ? ProductAdminComponent : ProductDetailComponent
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.