【发布时间】:2020-03-08 07:34:58
【问题描述】:
假设我在参数 id=1 的 Tab1。具体来说,生成的 url 是http://localhost:4200/project/tab1;id=1。现在我单击 Tab2,url 变为http://localhost:4200/project/tab2。有没有办法路由 id 以便我得到http://localhost:4200/project/tab2;id=1?
https://stackblitz.com/edit/angular-qzwzju
<nav mat-tab-nav-bar [backgroundColor]="'primary'">
<div mat-tab-link *ngFor="let link of navTabs" [routerLink]="link.link" routerLinkActive #rla="routerLinkActive"
[active]="rla.isActive">
{{link.label}}
</div>
</nav>
<router-outlet></router-outlet>
navTabs: NavTab[];
activeLinkIndex: number = -1;
constructor(private router: Router, private route: ActivatedRoute) {
this.navTabs = [
{ label: 'Tab1', link: './tab1', index: 0 },
{ label: 'Tab2', link: './tab2', index: 1 },
{ label: 'Tab3', link: './tab3', index: 2 },
];
}
ngOnInit(): void {
this.router.events.subscribe((res) => {
this.activeLinkIndex = this.navTabs.indexOf(this.navTabs.find((tab: NavTab) => tab.link === '.' + this.router.url));
});
}
export const appRoutes: Routes = [
{ path: 'tab1', component: Tab1Component },
{ path: 'tab2', component: Tab2Component },
{ path: 'tab3', component: Tab3Component },
];
【问题讨论】:
-
你能在 stackblitz 上创建一个演示吗??
-
这是一个演示,但不知何故它不适用于 stackblitz。 stackblitz.com/edit/angular-qzwzju
标签: angular