reactjs - 如何在 Ant-design/icons 中使用 V4 中的图标
问题描述
我试图制作一个菜单,所以我创建了一个 menuList 来使用 getMenuNodes() 配置菜单,但是 Ant 框架已从 v3 升级到 v4,图标方法已更改。他们现在使用icon={<PieChartOutlined />}
to 而不是icon='PieChartOutlined'
,一切正常,图标区域<PieChartOutlined />
现在显示该单词。我不知道为什么会这样,请帮我解决这个问题。
左导航.js
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import logo from '../../assets/images/logo.png';
import './index.less';
import { Menu } from 'antd';
import { PieChartOutlined } from '@ant-design/icons';
import menuList from '../../config/menuConfig';
const { SubMenu } = Menu;
export default class LeftNav extends Component {
getMenuNodes = menuList => {
return menuList.map(item => {
if (!item.children) {
return (
<Menu.Item key={item.key} icon={item.icon}>
<Link to={item.key}>{item.title}</Link>
</Menu.Item>
);
} else {
return (
<SubMenu key={item.key} icon={item.icon} title={item.title}>
{this.getMenuNodes(item.children)}
</SubMenu>
);
}
});
};
render() {
return (
<div className="left-nav">
<Link to="./" className="left-nav-header">
<img src={logo} alt="" />
<h1>Backend System</h1>
</Link>
<Menu
mode="inline"
theme="dark"
>
{this.getMenuNodes(menuList)}
</Menu>
</div>
);
}
}
菜单列表.js
const menuList = [
{
title: 'Home',
key: '/home',
icon: '<PieChartOutlined />',
},
{
title: 'Item',
key: '/products',
icon: '<PieChartOutlined />',
children: [
{
title: 'Category Control',
key: '/category',
icon: '<PieChartOutlined />',
},
{
title: 'Product Control',
key: '/product',
icon: '<PieChartOutlined />',
},
],
},
{
title: 'User Control',
key: '/user',
icon: '<PieChartOutlined />',
},
{
title: 'Role Control',
key: '/role',
icon: '<PieChartOutlined />',
},
{
title: 'Diagram',
key: '/charts',
icon: '<PieChartOutlined />',
children: [
{
title: 'Bar',
key: '/charts/bar',
icon: '<PieChartOutlined />',
},
{
title: 'Line',
key: '/charts/line',
icon: '<PieChartOutlined />',
},
{
title: 'Pie',
key: '/charts/pie',
icon: '<PieChartOutlined />',
},
],
},
];
export default menuList;
解决方案
您正在传递一个字符串'<PieChartOutlined />'
,您需要直接传递组件。
import { PieChartOutlined } from '@ant-design/icons';
和:
{
title: 'Product Control',
key: '/product',
icon: <PieChartOutlined />,
},
如果您还没有安装 ant-design/icons,则需要:
npm install --save @ant-design/icons
推荐阅读
- javascript - JavaScript 中的 Increment Month 使用 setMonth() 删除 DATE 格式
- regex - 全名的正则表达式优化
- r - 根据存储在另一个列表中的索引访问列表中的多个元素
- typo3 - sys_categories FAL 图像编辑器
- sql - Yii2:尝试使用自定义 SQL 查询获取非对象的属性
- asp.net-mvc - 将文件保存到当前用户的路径桌面
- javascript - 使用 redux connect 的连接组件的子组件在一次 prop 更改后不会重新渲染
- python - django 在 UpdateView 中提交多个表单
- android - 几次点击后音频文件停止在列表视图上播放?
- xslt - XSL - 用另一个字符替换管道