【问题标题】:How to implement breadcrumb navigation in Angular 4如何在 Angular 4 中实现面包屑导航
【发布时间】:2018-05-16 08:34:03
【问题描述】:

我想用下面的菜单实现面包屑导航

首页 > 解决方案 > 结果 > 联系方式

目前,我正在使用以下路由代码

  import { NgModule } from '@angular/core';
    import { RouterModule, Routes } from '@angular/router';

    import { SolutionComponent } from "./solutions.component";
    import { ResultComponent } from "./result/result.component";
    import { ContactComponent } from './contact/contact.component';


    const routes: Routes = [
        {
            path: 'home/solutions',
            component: SolutionsComponent
        },
        {
            path: 'home/solutions/result',
            component:ResultComponent
        },
        {
            path: 'home/solutions/result/contact',
            component: ContactComponent
        }
];
    @NgModule({
      imports: [RouterModule.forRoot(routes)],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }

任何人都可以建议示例 plunker 在 Angular 4 中实现面包屑导航

我创建了一个部分工作的 plunker https://plnkr.co/edit/iGLJ06zRVYWDYedAtsDW?p=preview

谢谢

【问题讨论】:

标签: angular breadcrumbs


【解决方案1】:
setBreadcrumb() {
    this.router.events.subscribe((val) => {
        this.displayBreadcrumbList = [];
        if (location.pathname !== '') {
            this.route = location.pathname;
            this.masterBreadcrumbList = this.route.split('/');
            this.masterBreadcrumbList = this.masterBreadcrumbList.slice(1, this.masterBreadcrumbList.length);
            for (let i = 0; i < this.masterBreadcrumbList.length; i++) {
                if (i !== 0) {
                    this.initialUrl = this.displayBreadcrumbList[i - 1];
                } else {
                    this.initialUrl = '/';
                }
                const breadCrumbObj = {
                    name: this.masterBreadcrumbList[i],
                    url: this.initialUrl + this.masterBreadcrumbList[i],
                    id: i
                };
                this.displayBreadcrumbList.push(breadCrumbObj);
            }
        } else {
            this.route = 'Home';
        }
    });
}
<ol class="breadcrumb">
        <li class="breadcrumb-item"><a class="breadcrumb-text" href="#">Home</a></li>
        <li *ngFor="let breadLink of displayBreadcrumbList" class="breadcrumb-item active" aria-current="page">
            <a href="javascript:void(0)" [routerLink]="breadLink.url" class="breadcrumb-text">{{breadLink.name }}</a></li>
    </ol>

【讨论】:

  • 只是骆驼案不见了
  • 你是对的,你应该添加 vars 的 decleration :-- export class BreadcrumbComponent implements OnInit { displayBreadcrumbList:Array;路线:字符串='';初始网址:字符串=''; masterBreadcrumbList:Array ;构造函数(私有路由器:路由器) { } ngOnInit(): void { this.setBreadcrumb(); }
【解决方案2】:

这样做:

const routes: Routes = [
        {
            path: 'home/solutions',
            component: SolutionsComponent,
            data: { breadcrumb: 'sample breadcrumb' },
        },
        {
            path: 'home/solutions/result',
            component:ResultComponent,
            data: { breadcrumb: 'sample breadcrumb' },
        },
        {
            path: 'home/solutions/result/contact',
            component: ContactComponent,
            data: { breadcrumb: 'sample breadcrumb' },
        }
];

【讨论】:

  • 谢谢,但如果你能为此创建一个 plunker 将会非常有用,我已经尝试使用 ng2-breadcrumbs 但它不起作用
  • 我已经创建了一个部分工作的 plunker,这是 plunker 的链接 (plnkr.co/edit/iGLJ06zRVYWDYedAtsDW?p=preview),谢谢
  • 如何用 ng5-breadcrumb 实现面包屑,我正在尝试使用 ng5-breadcrumb
【解决方案3】:

任何面包屑解决方案都必须处理 -

  1. 在路由中定义面包屑的声明式方法
  2. 通过不同标签更新任何路由的动态异步方式
  3. 在面包屑中跳过特定路线的方法

我创建了一个 Angular 库来处理所有这些问题,称为 xng-breadcrumbs - https://github.com/udayvunnam/xng-breadcrumb

请随意查看,开发的一个 Angular 应用程序显示了面包屑的使用 - https://xng-breadcrumb.netlify.com

Angular 库规划、创建、持续发布很好documented in this medium article

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多