高亮菜单
This commit is contained in:
@ -9,11 +9,6 @@ import './layout.less';
|
|||||||
|
|
||||||
const LayoutIndex: React.FC = (props) => {
|
const LayoutIndex: React.FC = (props) => {
|
||||||
const { children } = props;
|
const { children } = props;
|
||||||
console.log('主布局渲染:', {
|
|
||||||
children: !!children,
|
|
||||||
type: children ? typeof children : 'none'
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Layout>
|
<Layout>
|
||||||
|
@ -36,8 +36,8 @@ const generateMenuItems = (): IMenuItem[] => {
|
|||||||
// 递归处理路由,生成菜单项
|
// 递归处理路由,生成菜单项
|
||||||
const processRoutes = (routes: IRouteItem[], parentPath: string = ''): IMenuItem[] => {
|
const processRoutes = (routes: IRouteItem[], parentPath: string = ''): IMenuItem[] => {
|
||||||
return routes
|
return routes
|
||||||
.filter(route => !route.redirect && route.name && !route.meta?.hide)
|
.filter((route) => !route.redirect && route.name && !route.meta?.hide)
|
||||||
.map(route => {
|
.map((route) => {
|
||||||
// 构建完整路径
|
// 构建完整路径
|
||||||
const routePath = route.path.startsWith('/')
|
const routePath = route.path.startsWith('/')
|
||||||
? route.path
|
? route.path
|
||||||
@ -84,51 +84,9 @@ const SiderMenu: React.FC = (props: any) => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// 获取当前激活菜单
|
// 获取当前激活菜单
|
||||||
const path = history.location.pathname;
|
const path = history.location.pathname;
|
||||||
|
if (path.split('/').length > 1) {
|
||||||
// 递归查找匹配的菜单项
|
setCurrent(path.split('/')[path.split('/').length - 1]);
|
||||||
const findMatchingMenuItem = (menuItems: IMenuItem[]): string | null => {
|
return;
|
||||||
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');
|
|
||||||
}
|
}
|
||||||
}, [history.location.pathname]);
|
}, [history.location.pathname]);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user