修改header组件激活菜单方法

This commit is contained in:
linxd
2025-07-17 15:34:44 +08:00
parent 98de83d5f1
commit 0af9171acd

View File

@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { Menu } from 'antd'; import { Menu } from 'antd';
import Language from './Language'; import Language from './Language';
import { useIntl, Link, useHistory } from 'umi'; import { useIntl, Link, useHistory, useLocation } from 'umi';
import User from './User'; import User from './User';
interface IMenuItem { interface IMenuItem {
label: string; label: string;
@ -51,19 +51,17 @@ const HeaderMenu: React.FC = (props) => {
//当前激活菜单 //当前激活菜单
const [current, setCurrent] = useState('index'); const [current, setCurrent] = useState('index');
const intl = useIntl(); const intl = useIntl();
const history = useHistory(); const location = useLocation();
useEffect(() => { useEffect(() => {
// 获取当前激活菜单 const path = location.pathname;
const path = history.location.pathname;
const menu = items.find((item) => item.path === path); const menu = items.find((item) => item.path === path);
if (menu) { if (menu) {
setCurrent(menu.key); setCurrent(menu.key);
}else{ } else {
// 如果跳转的详情页面获取根级激活菜单
const rootActiveMenu = path.split('/')[1]; const rootActiveMenu = path.split('/')[1];
setCurrent(rootActiveMenu); setCurrent(rootActiveMenu);
} }
}, [history.location.pathname]); }, [location.pathname]);
return ( return (
<div className="header-menu"> <div className="header-menu">
<Menu selectedKeys={[current]} mode="horizontal"> <Menu selectedKeys={[current]} mode="horizontal">