高亮菜单

This commit is contained in:
linxd
2025-06-24 10:19:30 +08:00
parent 31208fa9ff
commit ca3d1b251b
2 changed files with 5 additions and 52 deletions

View File

@ -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 (
<>
<Layout>

View File

@ -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]);