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模块。

相关内容

热门资讯

微扑克ai机器人!微扑克可以在... 微扑克ai机器人!微扑克可以在软件内设置(透明挂)竟然是真的有挂;微扑克是一种具有地方特色的麻将游戏...
一起来讨论!欢乐贰柒拾辅助工具... 一起来讨论!欢乐贰柒拾辅助工具(辅助)确实真的是有挂(2025已更新)(哔哩哔哩)一、欢乐贰柒拾辅助...
wpk教程!we辅助poker... wpk教程!we辅助poker德之星(wepoke智能ai)we-poker(好像真的有挂)准备好在...
1分钟了解!微信雀神小程序辅助... 1分钟了解!微信雀神小程序辅助app,中至万年麻将挂在哪里,介绍教程(有挂神器)1、不需要AI权限,...
微扑克系统发牌规律!微扑克德州... 微扑克系统发牌规律!微扑克德州专用辅助器(透视)都是真的是有挂微扑克辅助器中分为三种模型:微扑克软件...
每日必看教程!天天真人麻将有挂... 每日必看教程!天天真人麻将有挂吗(辅助)确实是有挂(2023已更新)(哔哩哔哩)1、许多玩家不知道天...
wepoke辅助挂!wopok... wepoke辅助挂!wopoker系统机制(透明挂)wEPoke(一般真的有挂);wopoker最新...
6分钟了解!浙江游戏大厅麻将有... 6分钟了解!浙江游戏大厅麻将有挂吗,中至江西有挂吗,详细教程(有挂透视)1、玩家可以在浙江游戏大厅麻...
微扑克ai辅助工具!微扑克德州... 微扑克ai辅助工具!微扑克德州专用辅助器,微扑克系统发牌规律(好像真的是有挂)是一款可以让一直输的玩...
八分钟了解!老友二打一财神有用... 八分钟了解!老友二打一财神有用吗,欢聚水鱼怎么提高胜率,教你攻略(有挂总结);1、金币登录送、破产送...