当前位置: 首页 > news >正文

Tdesign-React 模板面包屑如何放到 Header头部

1、问题点:Tdesign-React 模板面包屑如何放到 Header头部

2、实现效果

3、源代码分析,官方通过路由的方式、为 Page 组件赋值。

4、实现思路,在 AppLayout.tsx 文件中为每个 header 传参 breadcrumbs(面包屑),关键点在于如何取到 breadcrumbs 列表值。

这里我使用 codeBuddy (deepseek-v3),输入需求:

我需要在该文件添加面包屑,帮我改造代码,并引入面包屑 breadcrumbs 值。保持跟 Page.tsx 一样的效果

deepseek-v3 给出的关键代码:

  const location = useLocation()

  const breadcrumbs = getBreadcrumbs(location.pathname);


// 获取当前路径的面包屑
const getBreadcrumbs = (pathname: string): string[] => {// console.log('pathname=>',pathname) // /dashboard/baseconst breadcrumbs: string[] = [];let currentPath = '';// 分割路径const pathSegments = pathname.split('/').filter(Boolean);// 遍历路径段,构建面包屑for (let i = 0; i < pathSegments.length; i++) {const segment = pathSegments[i];currentPath = currentPath ? `${currentPath}/${segment}` : `/${segment}`;// 在路由配置中查找匹配的路由const findRouteTitle = (routes: IRouter[], path: string): string | undefined => {for (const route of routes) {const routePath = resolve(path, route.path);if (routePath === currentPath && route.meta?.title) {return route.meta.title;}if (route.children) {const childTitle = findRouteTitle(route.children, routePath);if (childTitle) return childTitle;}}return undefined;};const title = findRouteTitle(routers, '') || segment;breadcrumbs.push(title);}return breadcrumbs;
};

这里 useLocation  是 React 的知识点。作用:获取当前路由的 location 对象 返回对象结构:

http://www.lryc.cn/news/605181.html

相关文章:

  • MongoDB系列教程-第三章:PyMongo操作MongoDB数据库(1)—— 连接、基本CRUD操作
  • 容器化与Docker核心原理
  • Odoo 18 PWA 全面掌握:从架构、实现到高级定制
  • SpringBoot中ResponseEntity的使用详解
  • 从一开始的网络攻防(十三):WAF入门到上手
  • 基于 Flexible.js + postcss-px-to-viewport 的 REM 适配方案(支持系统缩放与浏览器缩放)
  • SpringBoot+Three.js打造3D看房系统
  • ts 基础知识总结
  • 深入理解PostgreSQL的MVCC机制
  • 【自动化运维神器Ansible】Ansible常用模块之group模块详解
  • C++反射
  • 中大网校社会工作师培训创新发展,多维度赋能行业人才培养
  • vue+elementui+vueCropper裁剪上传图片背景颜色为黑色解决方案
  • OriGene:一种可自进化的虚拟疾病生物学家,实现治疗靶点发现自动化
  • Java 笔记 封装(Encapsulation)
  • vulhub-Thales靶场攻略
  • LRU (Least Recently Used) 缓存实现及原理讲解
  • Python读取获取波形图波谷/波峰
  • PSO-TCN-BiLSTM-MATT粒子群优化算法优化时间卷积神经网络-双向长短期记忆神经网络融合多头注意力机制多特征分类预测/故障诊断Matlab实现
  • Undo、Redo、Binlog的相爱相杀
  • 2025年华为HCIA-AI认证是否值得考?还是直接冲击HCIP?
  • 鸿蒙(HarmonyOS)模拟(Mock)数据技术
  • NestJS CLI入门
  • HPCtoolkit的下载使用
  • 7.Origin2021如何绘制拟合数据图?
  • 网络安全学习第16集(cdn知识点)
  • python 中 `batch.iloc[i]` 是什么:integer location
  • 【MySQL 数据库】MySQL索引特性(一)磁盘存储定位扇区InnoDB页
  • NEG指令说明
  • Android补全计划 TextView设置文字不同字体和颜色