AngularPrimeNG面包屑不会动态改变
创始人
2024-10-28 22:31:02
0

要动态更改面包屑,您需要使用AsyncPipe以异步方式从Observable获取路由事件。以下是一个代码示例:

在组件类中:

import { Component } from '@angular/core';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { filter, map } from 'rxjs/operators';
import { MenuItem } from 'primeng/api';

@Component({
  selector: 'app-breadcrumb',
  templateUrl: './breadcrumb.component.html',
  styleUrls: ['./breadcrumb.component.css']
})
export class BreadcrumbComponent {
  items: MenuItem[];

  constructor(private router: Router, private activatedRoute: ActivatedRoute) {
    this.router.events.pipe(
      filter(event => event instanceof NavigationEnd),
      map(() => this.activatedRoute),
      map(route => {
        while (route.firstChild) route = route.firstChild;
        return route;
      }),
      filter(route => route.outlet === 'primary'),
      mergeMap(route => route.data),
      map(data => {
        if (data.title) {
          // If there is a breadcrumb override, use that instead of the title
          if(typeof data.title === 'function') {
            return data.title(this.activatedRoute.snapshot.params);
          }
          return data.title;
        } else {
          return null;
        }
      })
    ).subscribe((label: string) => {
      if (label) {
        this.items.push({label: label});
      }
    });
  }

  ngOnInit() {
    this.items = [];
  }
}

在组件模板中:


注意:确保已正确导入并安装了Primeng和rxjs模块。

相关内容

热门资讯

2024教程!杭州都莱有没有挂... 2024教程!杭州都莱有没有挂,新卡农辅助软件,玩家教程(存在有挂)-哔哩哔哩;1、让任何用户在无需...
玩家亲测!花城棋舍辅助,花花生... 您好:花花生活圈辅助工具安装教程这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...
我来教教大家!温州茶苑破解版,... 我来教教大家!温州茶苑破解版,新金龙辅助工具,玩家教你(有人有挂)-哔哩哔哩;亲,温州茶苑破解版这款...
一分钟了解!微乐家乡游戏攻略,... 一分钟了解!微乐家乡游戏攻略,填大坑辅助软件,wepoke教程(有挂秘籍)-哔哩哔哩>>您好:软件加...
今日公布!堆金城陕西辅助器,欢... 今日公布!堆金城陕西辅助器,欢乐达人破解器,扑克教程!(有挂技巧)-哔哩哔哩【无需打开直接搜索加薇1...
透视插件!杭州都莱有没有挂,新... 杭州都莱有没有挂开挂教程视频分享装挂详细步骤在当今的网络游戏中,杭州都莱有没有挂作为一种经典的娱乐方...
揭秘!五十k辅助,来玩app ... 揭秘!五十k辅助,来玩app 德州 辅助,技巧教程(发现有挂)-哔哩哔哩;无需打开直接搜索加薇136...
科普攻略!超级三加一辅助工具安... 科普攻略!超级三加一辅助工具安装,wepoker透视脚本免费app,我来教教你(有挂规律)-哔哩哔哩...
透视辅助!途游四川破解,pok... 透视辅助!途游四川破解,pokemmo辅助官网,揭秘教程(有挂助手)-哔哩哔哩;打开点击测试直接进入...
玩家必看分享!同城游破解版下载... 玩家必看分享!同城游破解版下载,海盗来了辅助器,必胜教程(有挂助手)-哔哩哔哩【无需打开直接搜索加薇...