我试着用6角来制作面包屑。文本可以工作,但是链接不能正常工作。
我想做这个。
Home >仪表板>统计(它可以工作)
Home -> xxx.com (它有效)
Dashborad -> ->(它能工作)
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>发布于 2019-07-04 06:50:33
你必须用先前的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-面包屑(https://github.com/udayvunnam/xng-breadcrumb)涵盖了所有的用例以及其他一些有用的特性。任何面包屑解决方案都必须处理
可以随意检查生产准备的角应用程序https://xng-breadcrumb.netlify.com中面包屑的使用情况
https://stackoverflow.com/questions/51987343
复制相似问题