diff --git a/src/layouts/HeaderMenu.tsx b/src/layouts/HeaderMenu.tsx index 670c336..4e93e8e 100644 --- a/src/layouts/HeaderMenu.tsx +++ b/src/layouts/HeaderMenu.tsx @@ -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{ - // 如果跳转的详情页面获取根级激活菜单 + } else { const rootActiveMenu = path.split('/')[1]; setCurrent(rootActiveMenu); } - }, [history.location.pathname]); + }, [location.pathname]); return (