您可以将 DetailsComponent 映射到/feature/details
路由并将该:id
路由用作 details 路由的子级。在 DetailsComponent 中,您将使用带有路由的材质选项卡
在路由模块中
const routes: Routes = [
{
path: 'feature',
children: [
{
path: 'list',
component: ListComponent
},
{
path: 'details',
component: DetailsComponent,
children: [
{
path: ':id',
component: TabIdComponent,
data:
{
title: 'Detail',
}
},
{
path: 'list',
component: ListComponent,
data:
{
title: 'List',
}
},
...
{
path: '',
redirectTo: 'list',
pathMatch: 'full'
},
]
},
...
]
}
];
在 details.component.html
<nav mat-tab-nav-bar>
<a mat-tab-link
*ngFor="let tab of tabs"
[routerLink]="tab.link"
routerLinkActive
#rla="routerLinkActive"
[active]="rla.isActive"
>
{{ tab.label }}
</a>
</nav>
<router-outlet></router-outlet>`
要从路由器配置中获取选项卡列表,请从ActivatedRoute
details.component.ts 中列出
public tabs: Tab[];
constructor(activatedRoute: ActivatedRoute) {
activatedRoute.paramMap.subscribe(value => {
this.tabs = new List(activatedRoute.snapshot.routeConfig.children)
.Where(r => r.path !== '')
.Select(r => new Tab(r.path, r.data.title)).ToArray();
});
}
...
export class Tab {
link: string;
label: string;
constructor(link: string, label: string) {
this.label = label;
this.link = link;
}
}
如果您不希望 ListComponent 在主出口中加载(即使导航到 时也不/feature/list
),您可以将其重定向到/feature/details/list
而不是使用为该路由定义组件
{
path: 'list',
redirectTo: 'details/list',
pathMatch: 'full'
}