1.按照之前的全局初始菜单数据的方式来加载动态菜单,发现有一个bug,就是登录后,菜单数据不能正常显示(菜单位置空白),需要页面刷新一下,菜单才能正常显示。

    2.调试了一下,发现跳转到登录页面后,会执行app.jsx中的getInitialState方法,但是这时候没有正常登录,是没法获取到菜单信息的,但是点登录按钮以后,跳到首页以后并未执行getInitialState()方法获取菜单信息,刷新下页面后又会执行getInitialState(),加载菜单。

    3.未定位到具体的解决办法,又搜索了Antd pro V5的官网,从服务器加载 menu,官网示例代码中有一段示例代码如下:

    1. <ProLayout
    2. style={{
    3. height: '100vh',
    4. border: '1px solid #ddd',
    5. }}
    6. actionRef={actionRef}
    7. menu={{
    8. request: async () => {
    9. await waitTime(2000);
    10. return customMenuDate;
    11. },
    12. }}
    13. location={{
    14. pathname: '/welcome/welcome',
    15. }}
    16. >

    其中Layout的menu可以使用request异步加载服务端的菜单信息。

    4.按官网的示例重新修改app.jsx,去掉之前的fetchUserMenus,参照官网示例,改在Layout中使用menu获取菜单信息,同时保留原来的图标封装方法,修改后的app.jsx完整代码如下:

    1. import { PageLoading } from '@ant-design/pro-layout';
    2. import { history, Link } from 'umi';
    3. import RightContent from '@/components/RightContent';
    4. import Footer from '@/components/Footer';
    5. import { currentUser as queryCurrentUser } from './services/ant-design-pro/api';
    6. import { BookOutlined, LinkOutlined } from '@ant-design/icons';
    7. import { requestInterceptors, responseInterceptors, errorHandler } from '@/utils/Request';
    8. import { getCurrentUserMenus } from './services/ant-design-pro/menu';
    9. import fixMenuItemIcon from '@/utils/fixMenuItemIcon';
    10. const isDev = process.env.NODE_ENV === 'development';
    11. const loginPath = '/user/login';
    12. /** 获取用户信息比较慢的时候会展示一个 loading */
    13. export const initialStateConfig = {
    14. loading: <PageLoading />,
    15. };
    16. /**
    17. * @see https://umijs.org/zh-CN/plugins/plugin-initial-state
    18. * */
    19. export async function getInitialState() {
    20. const fetchUserInfo = async () => {
    21. try {
    22. const msg = await queryCurrentUser();
    23. console.log("login user info:",msg);
    24. return msg.user;
    25. } catch (error) {
    26. history.push(loginPath);
    27. }
    28. return undefined;
    29. };
    30. if (history.location.pathname !== loginPath) {
    31. const token = localStorage.getItem('access_token');
    32. if (!token) {
    33. history.push(loginPath);
    34. return {
    35. fetchUserInfo,
    36. currentUser:{},
    37. settings: {},
    38. };
    39. }
    40. const currentUser = await fetchUserInfo();
    41. return {
    42. fetchUserInfo,
    43. currentUser,
    44. settings: {},
    45. };
    46. }
    47. return {
    48. fetchUserInfo,
    49. currentUser,
    50. settings: {},
    51. };
    52. }
    53. // ProLayout 支持的api https://procomponents.ant.design/components/layout
    54. export const request = {
    55. errorHandler,
    56. requestInterceptors: [requestInterceptors],
    57. responseInterceptors: [responseInterceptors],
    58. };
    59. export const layout = ({ initialState,setInitialState }) => {
    60. return {
    61. rightContentRender: () => <RightContent />,
    62. disableContentMargin: false,
    63. waterMarkProps: {
    64. content: initialState?.currentUser?.name,
    65. },
    66. footerRender: () => <Footer />,
    67. onPageChange: () => {
    68. const { location } = history; // 如果没有登录,重定向到 login
    69. if (!initialState?.currentUser && location.pathname !== loginPath) {
    70. history.push(loginPath);
    71. }
    72. },
    73. links: isDev
    74. ? [
    75. <Link to="/umi/plugin/openapi" target="_blank">
    76. <LinkOutlined />
    77. <span>OpenAPI 文档</span>
    78. </Link>,
    79. <Link to="/~docs">
    80. <BookOutlined />
    81. <span>业务组件文档</span>
    82. </Link>,
    83. ]
    84. : [],
    85. menuHeaderRender: undefined,
    86. menu: {
    87. // 每当 initialState?.currentUser?.userId 发生修改时重新执行 request
    88. params: {
    89. userId: initialState?.currentUser?.userId,
    90. },
    91. request: async (params, defaultMenuData) => {
    92. // initialState.currentUser 中包含了所有用户信息
    93. const tempMenuData = await getCurrentUserMenus();
    94. const menuData=fixMenuItemIcon(tempMenuData);
    95. return menuData;
    96. },
    97. },
    98. ...initialState?.settings,
    99. };
    100. };

    其中去掉了之前的fetchUserMenus相关代码,在layout中获取后台菜单数据并加载显示的核心代码如下:

    1. menu: {
    2. // 每当 initialState?.currentUser?.userid 发生修改时重新执行 request
    3. params: {
    4. userId: initialState?.currentUser?.userId,
    5. },
    6. request: async (params, defaultMenuData) => {
    7. // initialState.currentUser 中包含了所有用户信息
    8. const tempMenuData = await getCurrentUserMenus();
    9. const menuData=fixMenuItemIcon(tempMenuData);
    10. return menuData;
    11. },
    12. },

    其中getCurrentUserMenus和fixMenuItemIcon之前已做过封装。

    5.前台代码修改后,重新编译运行,登录后菜单正常显示。