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