From ca3d1b251ba91905dbc1d9e26a9f9691e8f7527d Mon Sep 17 00:00:00 2001 From: linxd <544554903@qq.com> Date: Tue, 24 Jun 2025 10:19:30 +0800 Subject: [PATCH] =?UTF-8?q?=E9=AB=98=E4=BA=AE=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layouts/Index.tsx | 5 ---- src/layouts/SiderMenu.tsx | 52 ++++----------------------------------- 2 files changed, 5 insertions(+), 52 deletions(-) diff --git a/src/layouts/Index.tsx b/src/layouts/Index.tsx index 81bdb3b..1bac49f 100644 --- a/src/layouts/Index.tsx +++ b/src/layouts/Index.tsx @@ -9,11 +9,6 @@ import './layout.less'; const LayoutIndex: React.FC = (props) => { const { children } = props; - console.log('主布局渲染:', { - children: !!children, - type: children ? typeof children : 'none' - }); - return ( <> diff --git a/src/layouts/SiderMenu.tsx b/src/layouts/SiderMenu.tsx index 7a2eddb..d5fbfe6 100644 --- a/src/layouts/SiderMenu.tsx +++ b/src/layouts/SiderMenu.tsx @@ -36,8 +36,8 @@ const generateMenuItems = (): IMenuItem[] => { // 递归处理路由,生成菜单项 const processRoutes = (routes: IRouteItem[], parentPath: string = ''): IMenuItem[] => { return routes - .filter(route => !route.redirect && route.name && !route.meta?.hide) - .map(route => { + .filter((route) => !route.redirect && route.name && !route.meta?.hide) + .map((route) => { // 构建完整路径 const routePath = route.path.startsWith('/') ? route.path @@ -84,51 +84,9 @@ const SiderMenu: React.FC = (props: any) => { useEffect(() => { // 获取当前激活菜单 const path = history.location.pathname; - - // 递归查找匹配的菜单项 - const findMatchingMenuItem = (menuItems: IMenuItem[]): string | null => { - for (const item of menuItems) { - if (item.path === path) { - return item.key; - } - if (item.children) { - const childMatch = findMatchingMenuItem(item.children); - if (childMatch) return childMatch; - } - } - return null; - }; - - const matchedKey = findMatchingMenuItem(items); - if (matchedKey) { - setCurrent(matchedKey); - } else { - // 如果没有精确匹配,尝试匹配路径前缀 - const pathParts = path.split('/'); - if (pathParts.length > 1) { - const basePath = `/${pathParts[1]}`; - const findByPrefix = (menuItems: IMenuItem[]): string | null => { - for (const item of menuItems) { - if (item.path === basePath) { - return item.key; - } - if (item.children) { - const childMatch = findByPrefix(item.children); - if (childMatch) return childMatch; - } - } - return null; - }; - - const prefixMatch = findByPrefix(items); - if (prefixMatch) { - setCurrent(prefixMatch); - return; - } - } - - // 默认选中首页 - setCurrent('index'); + if (path.split('/').length > 1) { + setCurrent(path.split('/')[path.split('/').length - 1]); + return; } }, [history.location.pathname]);