This is how i have implemented.
app.component.html
<nav mat-tab-nav-bar>
<a mat-tab-link
*ngFor="let link of navLinks"
[routerLink]="link.link"
routerLinkActive #rla="routerLinkActive"
[active]="rla.isActive">
{{link.label}}
</a>
</nav>
<router-outlet></router-outlet>
AppComponent
export class AppComponent implements OnInit{
title = 'app';
navLinks: any[];
activeLinkIndex = -1;
constructor(private router: Router) {
this.navLinks = [
{
label: 'TabTest1',
link: './tabtest1',
index: 0
}, {
label: 'Tab Test2',
link: './tabtest2',
index: 1
}, {
label: 'Tab Test3',
link: './tabtest3',
index: 2
},
];
}
ngOnInit(): void {
this.router.events.subscribe((res) => {
this.activeLinkIndex = this.navLinks.indexOf(this.navLinks.find(tab => tab.link === '.' + this.router.url));
});
}
}
routing.module.ts
const routes: Routes = [
{ path: '', redirectTo: '/tabtest1', pathMatch: 'full' },
{ path: 'tabtest1', component: TestComponent1},
{ path: 'tabtest2', component: TestComponent2},
];
export const appRouting = RouterModule.forRoot(routes);
@NgModule({
imports: [
RouterModule.forRoot(routes),
CommonModule
],
exports:[
RouterModule
],
declarations: []
})
export class AppRoutingModule { }
I hope this helps someone