I try to make breadcrumb with Angular 6. Texts work but links doesn't work correctly.
I want to do this.
Home > Dashboard > Statistical ( It Works.)
Home -> xxx.com (It works.)
Dashborad -> xxx.com/dashboard (It works.)
Statistical -> xxx.com/statistical (It doesn't work.)
So, if it is subcomponent links don't work.
It needs to be xxx.com/dashboard/statistical (Correct)
How can i do this ?
breadcrumb.component.ts
import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { Router } from '@angular/router';
@Component({
selector: 'app-breadcrumb',
templateUrl: './breadcrumb.component.html',
styleUrls: ['./breadcrumb.component.scss']
})
export class BreadcrumbComponent implements OnInit {
route: string;
breadcrumbList: Array<any>;
routeLinks: number;
count: number;
constructor(location: Location, router: Router) {
router.events.subscribe((val) => {
if (location.path() !== '') {
this.route = location.path();
this.breadcrumbList = this.route.split('/');
this.count = this.breadcrumbList.length;
} else {
this.route = 'Home';
}
});
}
ngOnInit() {}
}
breadcrumb.component.html
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<ng-container *ngIf="count == null">
<li class="breadcrumb-item"><i class="material-icons">home</i></li>
</ng-container>
<ng-container *ngIf="count != null">
<li class="breadcrumb-item"><a href="javascript:void(0)" [routerLink]=""><i class="material-icons">home</i></a></li>
</ng-container>
<ng-container *ngFor="let breadLink of breadcrumbList">
<li class="breadcrumb-item"><a href="javascript:void(0)" [routerLink]="breadcrumbList[2]">{{breadLink}}</a></li>
</ng-container>
</ol>
</nav>