javascript - 如何在 Angular 6 上制作面包屑

标签 javascript typescript angular6 breadcrumbs

我尝试使用 Angular 6 制作面包屑。文本有效,但链接无法正常工作。

我想做这个。

主页 > 仪表板 > 统计(有效。)

主页 -> xxx.com(有效。)

Dashborad -> xxx.com/dashboard(有效。)

Statistical -> 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>

最佳答案

你必须重建每个 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

关于javascript - 如何在 Angular 6 上制作面包屑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51987343/

相关文章:

typescript - 映射类型可以使属性成为可选的,但前提是满足条件?

javascript - 在 app init 上检查本地存储的 Angular 6 问题

javascript - 在 React 中使用 zindex 将 div 元素置于前面

javascript - AngularJs 在 ng-repeat 中设置单个点击元素的样式

javascript - ngx-editor 链接重定向到与 Angular 项目 url 相结合的 url

angular - 由于 http 拦截器 Angular 6 中的错误,无法构建 Angular 应用程序

javascript - 如何使用 TypeScript null 检查不向 Date 对象传递任何值

javascript - 如何知道我点击了哪个元素触发了模糊事件处理程序?

angular - 在 mat-option 中使用 [(indeterminate)] 属性

angular - 不能在 Angular 6 的子模块中使用在应用程序模块内声明的指令