Ant Design Pro V4.5 从服务器请求菜单图标不显示解决

如果发现只有菜单,没有图标,只需要在models下的menu.ts中天下如下内容

import allIcons from '@@/plugin-antd-icon/icons';
import React from 'react';

const toHump = (name: string) =>
  name.replace(/-(\w)/g, (all: string, letter: any) => letter.toUpperCase());
const formatter = (data: any[]) => {
  data.forEach(item => {
    if (item.icon) {
      const { icon } = item;
      const v4IconName = toHump(icon.replace(icon[0], icon[0].toUpperCase()));
      const NewIcon = allIcons[icon] || allIcons[''.concat(v4IconName, 'Outlined')];
      if (NewIcon) {
        try {
          // eslint-disable-next-line no-param-reassign
          item.icon = React.createElement(NewIcon);
        } catch (error) {
          console.log(error);
        }
      }
    }
    if (item.routes || item.children) {
      const children = formatter(item.routes || item.children); // Reduce memory usage
      item.children = children;
    }
  });
  return data;
};

然后再函数返回前调用上面的函数,就可以了,是不是很简单 

//这里做了个转换,可能服务端返回的接口格式和前端的路由格式并不一致,可以在这个方法里进行调整,这里的方法仅作参考,根据自己实际情况进行调整即可
const menuFormatter = (response: any) => {

    if (response === null)
    {
        console.log("response === null");
        return [];
    }
    else if(response === undefined)
    {
        console.log("response === undefined");
        return [];
    }
    return formatter(response);
}

如果还是有图标不显示,在models下创建icons.ts文件,导入你需要的图标即可

import SlidersOutlined from '@ant-design/icons/SlidersOutlined';
import DesktopOutlined from '@ant-design/icons/DesktopOutlined';
import TableOutlined from '@ant-design/icons/TableOutlined';
import ToolOutlined from '@ant-design/icons/ToolOutlined'
import StepBackwardOutlined from '@ant-design/icons/StepBackwardOutlined'
import StepForwardOutlined from '@ant-design/icons/StepForwardOutlined'
import FastBackwardOutlined from '@ant-design/icons/FastBackwardOutlined'
import EditOutlined from '@ant-design/icons/EditOutlined'

export default {
SlidersOutlined,
DesktopOutlined,
TableOutlined,
ToolOutlined,
EditOutlined,
StepBackwardOutlined,
StepForwardOutlined,
FastBackwardOutlined,
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花开花落的个人博客

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值