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

react+antdesign达成后台管理系统面包屑

在这里插入图片描述
andDesign面包屑组件,实现这个功能主要是当我点击对应目录下的子菜单使用递归获取当前叶子节点的最高父节点拿到整个对象塞到集合中去

创建 Breadcrumb组件

import { Breadcrumb
} from 'antd';
import { routes
} from '@/router/routes';
import { useLocation
} from'react-router-dom';
import { getBreadcrumbList
} from '@/utils';
const BreadcrumbNav = () =>
{
const location = useLocation();
const items = getBreadcrumbList(routes, location.pathname);
const breadcrumbItems = items.map(item =>
{
const childrenMenu = item.children?.map(child =>
({
key: child.path,
label: <a href={
`#${child.path
}`
}>
{child.meta.title
}<
/a>
})) || [];
return {
title: (
<span>{item?.meta?.title}</span>),menu: childrenMenu.length >0 ? { items: childrenMenu} : undefined};});return (<Breadcrumbitems={[{title: <a href="#/">首页</a>},...breadcrumbItems]}/>)};export default BreadcrumbNav;

utils文件

export const searchRoute = (path: string, routes: RouteObject[] = []): RouteObject =>
{
let result: RouteObject = {
};
for (const item of routes) {
if (item.path === path) return item;
if (item.children) {
const res = searchRoute(path, item.children);
if (Object.keys(res).length) result = res;
}
}
return result;
};
export const getBreadcrumbList = (routes: RouteObject[] = [], currentPath: string) =>
{
if(!currentPath || currentPath === '/') return [];
const currentParent = findBreadcrumb(routes, currentPath)
const current = searchRoute(currentPath, routes)
return [
currentParent,
current
]
}
export const findBreadcrumb = (routes: RouteObject[] = [], targetPath: string): any =>
{
// 用于存储找到的最外层父节点
let outermostParent = null;
// 递归遍历路由配置的函数
function traverse(nodes, currentParent?: any) {
for (const node of nodes) {
// 如果当前节点有子节点,继续递归遍历
if (node.children && node.children.length >
0) {
// 检查当前节点的子节点中是否包含目标路径
const hasTarget = node.children.some(child => child.path === targetPath);
// 如果找到目标路径,记录当前节点作为父节点
if (hasTarget) {
// 最外层父节点是首次找到的包含目标路径的祖先节点
if (!outermostParent) {
outermostParent = node;
}
} else {
// 否则继续深入查找,当前节点作为临时父节点
traverse(node.children, node);
}
}
}
}
// 从根节点开始遍历
traverse(routes, null);
return outermostParent;
};
http://www.rkmt.cn/news/9794.html

相关文章:

  • 详细介绍:深入理解 JVM 字节码文件:从组成结构到 Arthas 工具实践
  • (1-10-2)MyBatis 进阶篇 - 教程
  • Spark 性能优化全攻略:内存管理、shuffle 优化与参数调优 - 详解
  • 如何隐藏一个元素
  • 软工9.22
  • 在控制台执行可列出所有placeholder样式
  • 对于一门古老东欧玄学的初步研究的简要报告
  • Java学习笔记:从三个实验看编程思维的锤炼
  • 完整教程:App 上架平台全解析,iOS 应用发布流程、苹果 App Store 审核步骤
  • 题解:AT_arc068_d [ARC068F] Solitaire
  • Codeforces Round 1051 (Div. 2) D1D2题解
  • 深入解析:基于 Kubernetes 的湖仓一体架构部署指南
  • 完整教程:真空发生器的工作原理
  • 【分布式架构实战】Spring Cloud 与 Dubbo 深度对比:从架构到实战,谁才是微服务的王者? - 详解
  • 探展打卡 Serverless,2025 云栖大会来了
  • 贪心算法应用:多重背包启发式疑问详解
  • 划重点|云栖大会「AI 原生应用架构论坛」看点梳理
  • Margin 塌陷问题如何解决?触发BFC。BFC的概念和触发条件
  • 火速收藏!2025 云栖大会 AI 中间件议程看点全公开(附免费报名通道)
  • WinForm引入项目资源文件
  • 训练集,验证集,测试集
  • ESP32 读取旋转编码器
  • 数1的个数
  • 基于RSSI修正的定位算法分析
  • 接口测试流程+jmeter并发+面试题(总结) - 指南
  • 完整教程:Java多线程初阶
  • 当写脚本循环更新几百万数据发现很慢怎么办 - 孙龙
  • 服装采购跟单系统的高效管理实践 - 详解
  • 服务器CPU、内存、磁盘、网络使用率,东方通CPU使用率东方通内存使用率监控脚本
  • 什么是 AutoModel