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

相关内容

热门资讯

透视神器!德扑圈有透视吗,(德... 透视神器!德扑圈有透视吗,(德普)竟然真的有挂,2025新版技巧(有挂攻略)1、任何德扑圈有透视吗a...
透视辅助!哈糖大菠萝怎么开挂,... 透视辅助!哈糖大菠萝怎么开挂,poker master辅助,必胜教程(有挂辅助)1、金币登录送、破产...
透视线上!德普之星透视免费,德... 透视线上!德普之星透视免费,德普之星透视免费,可靠教程(有挂揭秘)该软件可以轻松地帮助玩家将德普之星...
透视最新!werplan免费挂... 透视最新!werplan免费挂下载,sohoo poker辅助,教你教程(有挂插件);1、完成wer...
透视黑科技!德普之星透视辅助插... 透视黑科技!德普之星透视辅助插件,(德扑之心)都是有挂,揭秘攻略(有挂介绍)1、完成德普之星透视辅助...
透视辅助“pokemmo脚本”... 透视辅助“pokemmo脚本”透视辅助助手(一直有挂);亲,关键说明,pokemmo脚本赛季回归,p...
透视存在!werplan外挂,... 透视存在!werplan外挂,德州局透视,必赢教程(有挂教程)所有人都在同一条线上,像星星一样排成一...
透视总结!德普之星有辅助软件吗... 透视总结!德普之星有辅助软件吗,德普辅助软件,2025新版(有挂解密)1)德普之星有辅助软件吗辅助挂...
透视神器!红龙poker辅助工... 透视神器!红龙poker辅助工具,拱趴大菠萝有挂吗,详细教程(有挂揭秘);1)拱趴大菠萝有挂吗辅助挂...
透视透视!德普之星透视辅助软件... 透视透视!德普之星透视辅助软件激活码,(德普)果然真的有挂,黑科技教程(有挂细节)运德普之星透视辅助...