1.在src/utils目录下新建一个fixMenuItemIcon.js文件,创建一个function fixMenuItemIcon来正常显示菜单图标

    2.复制以下代码到fixMenuItemIcon.js文件中

    1. import React from 'react';
    2. import * as allIcons from '@ant-design/icons';
    3. function fixMenuItemIcon(menus, iconType = 'Outlined') {
    4. if (menus && menus.length > 0) {
    5. menus.forEach((item) => {
    6. const { icon, children } = item;
    7. if (icon && typeof icon === 'string') {
    8. const fixIconName = icon.slice(0, 1).toLocaleUpperCase() + icon.slice(1) + iconType;
    9. let iconObject ='';
    10. if(typeof allIcons[fixIconName] !== 'undefined')
    11. {
    12. iconObject = React.createElement(allIcons[fixIconName]);
    13. }
    14. else if(typeof allIcons[icon] !== 'undefined')
    15. {
    16. iconObject = React.createElement(allIcons[icon]);
    17. }
    18. item.icon = iconObject;
    19. }
    20. children && children.length > 0 ? (item.children = fixMenuItemIcon(children)) : null;
    21. });
    22. }
    23. return menus;
    24. }
    25. export default fixMenuItemIcon;

    3.这段代码稍作说明,遍历所有菜单item,根据菜单的icon属性,创建对应的Outlined类型图标对象,改完后item.icon是一个真正的antd pro图标对象,如果图标名称是antd pro中不存在的图标,那么item.icon=’’;

    1. if (icon && typeof icon === 'string') {
    2. const fixIconName = icon.slice(0, 1).toLocaleUpperCase() + icon.slice(1) + iconType;
    3. let iconObject ='';
    4. if(typeof allIcons[fixIconName] !== 'undefined')
    5. {
    6. iconObject = React.createElement(allIcons[fixIconName]);
    7. }
    8. else if(typeof allIcons[icon] !== 'undefined')
    9. {
    10. iconObject = React.createElement(allIcons[icon]);
    11. }
    12. item.icon = iconObject;
    13. }

    4.如果菜单有子菜单,递归做同样的操作,即给菜单配上真正的图标
    children && children.length > 0 ? (item.children = fixMenuItemIcon(children)) : null;
    });

    5.fixMenuItemIcon.js写完后,我们要在src/app.jsx中调用一下,在app.jsx中引入fixMenuItemIcon

    1. import fixMenuItemIcon from '@/utils/fixMenuItemIcon';

    6.在 menuDataRender: () =>{ return initialState?.menuData },这行代码中加入fixMenuItemIcon方法

    1. menuDataRender: () =>{
    2. return fixMenuItemIcon(initialState?.menuData);
    3. },

    7.保存,等编译通过后,重新登录查看下菜单数据,顺利的话,菜单图标会正常显示,如下图
    image.png