Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧐[问题 | question] 关于V5动态菜单图标的优雅解决问题 #10158

Closed
Mankvis opened this issue Aug 22, 2022 · 5 comments
Closed

Comments

@Mankvis
Copy link

Mankvis commented Aug 22, 2022

🧐 问题描述 | Problem description

目前使用了动态菜单,发现动态菜单图标无法正常加载,经过查阅资料,找到以下解决问题的办法

💻 示例代码 | Sample code

import React from 'react';
import { MenuDataItem } from '@ant-design/pro-layout';
import * as allIcons from '@ant-design/icons';

// FIX从接口获取菜单时icon为string类型
const fixMenuItemIcon = (menus: MenuDataItem[], iconType = 'Outlined'): MenuDataItem[] => {
menus.forEach((item) => {
const { icon, children } = item;
if (typeof icon === 'string') {
let fixIconName = icon.slice(0, 1).toLocaleUpperCase() + icon.slice(1) + iconType;
item.icon = React.createElement(allIcons[fixIconName] || allIcons[icon]);
}
children && children.length > 0 ? (item.children = fixMenuItemIcon(children)) : null;
});
return menus;
};

export default fixMenuItemIcon;

在layout组件中直接调用
menuDataRender={() => fixMenuItemIcon(menus.menusData)}

但是使用了这个办法之后,会出现一下警告,大概意思就是说引入包的方式有问题

image

OS:

Node:

浏览器 | browser:

@github-actions
Copy link

github-actions bot commented Aug 22, 2022

以下的 Issues 可能会帮助到你 / The following issues may help you

@q269384828
Copy link

你这样引入所有的图标太大了.... 还不如手写一个icons.tsx 文件

@Mankvis
Copy link
Author

Mankvis commented Aug 29, 2022

你这样引入所有的图标太大了.... 还不如手写一个icons.tsx 文件

是有增大项目文件的问题,不过已经解决了这个警告,但是现在动态加载菜单的话二级菜单的图标不会显示,还在寻找解决办法

@chenshuai2144
Copy link
Collaborator

在动态加载菜单的话二级菜单的图标不会显示

这个是专门的,为了防止icon太多眼花缭乱

@HalcyonDaysssss
Copy link

你这样引入所有的图标太大了....还不如手写一个icons.tsx文件

是有较大项目文件的问题,不过已经解决了这个警告,但是现在在动态加载菜单的二级菜单的图标不会显示,还在寻找解决办法

怎么解决的呢大佬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants