我尝试使用Angular 6做面包屑。文本可以工作,但链接不能正常工作。
我想这样做。
首页>信息中心>统计信息(有效)
主页-> xxx.com (有效)
Dashborad-> xxx.com/dashboard (有效)。
统计-> xxx.com/statistical (无效)
因此,如果它是子组件链接,则不起作用。
它必须为 xxx.com/dashboard/statistical (正确)
我该怎么做?
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>
答案 0 :(得分:0)
您必须重新构建每个带前缀的URL的URL链接-
this.breadcrumbList = this.route.split('/');
this.breadcrumbLinksList = [this.breadcrumbList[0]];
for(let i=1; i<=this.breadcrumbList.length; i++){
const link = this.breadcrumbLinksList[i-1] +'/'+ this.breadcrumbList[i]
this.breadcrumbLinksList.push(link)
}
在html中用作-
<ng-container *ngFor="let breadcrumbLabel of breadcrumbList; let i=index">
<li class="breadcrumb-item">
<a [routerLink]="breadcrumbLinksList[i]">{{breadcrumbLabel}}</a>
</li>
</ng-container>
要更详尽地使用面包屑,我认为xng-breadcrumb(https://github.com/udayvunnam/xng-breadcrumb)涵盖了您的所有用例以及其他一些有用的功能。任何面包屑解决方案都必须处理
在生产就绪的Angular应用https://xng-breadcrumb.netlify.com中随意检查面包屑的使用情况