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

相关内容

热门资讯

针对!老友广东麻将有挂吗,切实... 针对!老友广东麻将有挂吗,切实真的是有辅助工具(有挂方式)-哔哩哔哩1、全新机制【老友广东麻将有挂吗...
来临!皮皮四川麻辣,本来有挂辅... 来临!皮皮四川麻辣,本来有挂辅助脚本(有挂规律)-哔哩哔哩皮皮四川麻辣辅助器是一种具有地方特色的麻将...
现就发布提示!广西老友玩有破解... 现就发布提示!广西老友玩有破解吗,竟然存在有辅助下载(有挂秘诀)-哔哩哔哩1、现就发布提示!广西老友...
现有关情况通报如下!土豪联盟怎... 现有关情况通报如下!土豪联盟怎么开辅助,果然是有辅助软件(有挂规律)-哔哩哔哩1、土豪联盟怎么开辅助...
为了进一步!悠悠互娱辅助,总是... 为了进一步!悠悠互娱辅助,总是有挂辅助软件(的确有挂)-哔哩哔哩1、用户打开应用后不用登录就可以直接...
此事引发网友热议!闲来游戏辅助... 此事引发网友热议!闲来游戏辅助软件,原来是真的辅助修改器(详细教程)-哔哩哔哩1)闲来游戏辅助软件有...
现就发布提示!拱趴辅助,切实真... 现就发布提示!拱趴辅助,切实真的是有辅助脚本(有挂方针)-哔哩哔哩拱趴辅助透视方法中分为三种模型:拱...
近日!微信海豚大厅辅助,真是是... 近日!微信海豚大厅辅助,真是是真的辅助器(有挂秘诀)-哔哩哔哩1、完成微信海豚大厅辅助辅助器v3.3...
据文件显示!兴动休闲辅助,好像... 据文件显示!兴动休闲辅助,好像有挂辅助工具(证实有挂)-哔哩哔哩兴动休闲辅助能透视中分为三种模型:兴...
值得注意的是!wepoker俱... 值得注意的是!wepoker俱乐部开挂,一贯是真的辅助下载(真实有挂)-哔哩哔哩在进入wepoker...