1.按照之前的全局初始菜单数据的方式来加载动态菜单,发现有一个bug,就是登录后,菜单数据不能正常显示(菜单位置空白),需要页面刷新一下,菜单才能正常显示。
2.调试了一下,发现跳转到登录页面后,会执行app.jsx中的getInitialState方法,但是这时候没有正常登录,是没法获取到菜单信息的,但是点登录按钮以后,跳到首页以后并未执行getInitialState()方法获取菜单信息,刷新下页面后又会执行getInitialState(),加载菜单。
3.未定位到具体的解决办法,又搜索了Antd pro V5的官网,从服务器加载 menu,官网示例代码中有一段示例代码如下:
<ProLayout
style={{
height: '100vh',
border: '1px solid #ddd',
}}
actionRef={actionRef}
menu={{
request: async () => {
await waitTime(2000);
return customMenuDate;
},
}}
location={{
pathname: '/welcome/welcome',
}}
>
其中Layout的menu可以使用request异步加载服务端的菜单信息。
4.按官网的示例重新修改app.jsx,去掉之前的fetchUserMenus,参照官网示例,改在Layout中使用menu获取菜单信息,同时保留原来的图标封装方法,修改后的app.jsx完整代码如下:
import { PageLoading } from '@ant-design/pro-layout';
import { history, Link } from 'umi';
import RightContent from '@/components/RightContent';
import Footer from '@/components/Footer';
import { currentUser as queryCurrentUser } from './services/ant-design-pro/api';
import { BookOutlined, LinkOutlined } from '@ant-design/icons';
import { requestInterceptors, responseInterceptors, errorHandler } from '@/utils/Request';
import { getCurrentUserMenus } from './services/ant-design-pro/menu';
import fixMenuItemIcon from '@/utils/fixMenuItemIcon';
const isDev = process.env.NODE_ENV === 'development';
const loginPath = '/user/login';
/** 获取用户信息比较慢的时候会展示一个 loading */
export const initialStateConfig = {
loading: <PageLoading />,
};
/**
* @see https://umijs.org/zh-CN/plugins/plugin-initial-state
* */
export async function getInitialState() {
const fetchUserInfo = async () => {
try {
const msg = await queryCurrentUser();
console.log("login user info:",msg);
return msg.user;
} catch (error) {
history.push(loginPath);
}
return undefined;
};
if (history.location.pathname !== loginPath) {
const token = localStorage.getItem('access_token');
if (!token) {
history.push(loginPath);
return {
fetchUserInfo,
currentUser:{},
settings: {},
};
}
const currentUser = await fetchUserInfo();
return {
fetchUserInfo,
currentUser,
settings: {},
};
}
return {
fetchUserInfo,
currentUser,
settings: {},
};
}
// ProLayout 支持的api https://procomponents.ant.design/components/layout
export const request = {
errorHandler,
requestInterceptors: [requestInterceptors],
responseInterceptors: [responseInterceptors],
};
export const layout = ({ initialState,setInitialState }) => {
return {
rightContentRender: () => <RightContent />,
disableContentMargin: false,
waterMarkProps: {
content: initialState?.currentUser?.name,
},
footerRender: () => <Footer />,
onPageChange: () => {
const { location } = history; // 如果没有登录,重定向到 login
if (!initialState?.currentUser && location.pathname !== loginPath) {
history.push(loginPath);
}
},
links: isDev
? [
<Link to="/umi/plugin/openapi" target="_blank">
<LinkOutlined />
<span>OpenAPI 文档</span>
</Link>,
<Link to="/~docs">
<BookOutlined />
<span>业务组件文档</span>
</Link>,
]
: [],
menuHeaderRender: undefined,
menu: {
// 每当 initialState?.currentUser?.userId 发生修改时重新执行 request
params: {
userId: initialState?.currentUser?.userId,
},
request: async (params, defaultMenuData) => {
// initialState.currentUser 中包含了所有用户信息
const tempMenuData = await getCurrentUserMenus();
const menuData=fixMenuItemIcon(tempMenuData);
return menuData;
},
},
...initialState?.settings,
};
};
其中去掉了之前的fetchUserMenus相关代码,在layout中获取后台菜单数据并加载显示的核心代码如下:
menu: {
// 每当 initialState?.currentUser?.userid 发生修改时重新执行 request
params: {
userId: initialState?.currentUser?.userId,
},
request: async (params, defaultMenuData) => {
// initialState.currentUser 中包含了所有用户信息
const tempMenuData = await getCurrentUserMenus();
const menuData=fixMenuItemIcon(tempMenuData);
return menuData;
},
},
其中getCurrentUserMenus和fixMenuItemIcon之前已做过封装。
5.前台代码修改后,重新编译运行,登录后菜单正常显示。