Ant Design Pro自定义菜单图标

Ant Design Pro支持直接修改菜单图标为ant-design/icons和iconfont图标,但实际开发中我们需要换上设计图中的图标,所以需要完全自定义菜单图标。

自定义步骤如下:

【第一步】在@/components/MenuIcon文件中引入svg图标

const summarize = () => (
  <svg
    width="14"
    height="12"
    viewBox="0 0 14 12"
    fill="none"
    stroke="currentColor"
    xmlns="http://www.w3.org/2000/svg"
  >
    <path
      d="M7.00033 8.33333V11.25M7.00033 8.33333L10.5003 11.25M7.00033 8.33333L3.50033 11.25M12.2503 0.75V5.53333C12.2503 6.51342 12.2503 7.00347 12.0596 7.37782C11.8918 7.7071 11.6241 7.97482 11.2948 8.14259C10.9205 8.33333 10.4304 8.33333 9.45033 8.33333H4.55033C3.57023 8.33333 3.08019 8.33333 2.70584 8.14259C2.37656 7.97482 2.10884 7.7071 1.94106 7.37782C1.75033 7.00347 1.75033 6.51342 1.75033 5.53333V0.75M4.66699 4.25V6M7.00033 3.08333V6M9.33366 5.41667V6M12.8337 0.75H1.16699"
      strokeLinecap="round"
      strokeLinejoin="round"
    />
  </svg>
);

export const IconMap: any = {
  summarize,
};

【第二步】在app.tsx中修改菜单显示的图标

import { IconMap } from '@/components/MenuIcon';
import Icon from '@ant-design/icons';

export const layout = () => {
  return {
    menuDataRender: (menuData: any) => {
      return menuData.map((item: any) => {
        return {
          ...item,
          icon:
            typeof item.icon === 'string' && item.icon.indexOf('|custom') > -1 ? (
              <Icon component={IconMap[item.icon.replace('|custom', '')]} />
            ) : (
              item.icon
            ),
        };
      });
    },
  };
};

【第三步】在routes.ts中设置菜单icon的名称(加上过滤自定义标签的标识,我这里加的是"|custom",改成其他的也一样)

{
  path: '/summarize',
  name: 'summarize',
  icon: 'summarize|custom',
  component: './Summarize',
},
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值