【问题标题】:how to use icon in Ant-design/icons with V4如何在 Ant-design/icons 中使用 V4 中的图标
【发布时间】:2021-01-23 01:48:39
【问题描述】:

我尝试制作一个菜单,所以我创建了一个 menuList 来使用 getMenuNodes() 配置菜单,但 Ant 框架已从 v3 升级到 v4,图标方法已更改。他们现在使用icon={<PieChartOutlined />} 代替icon='PieChartOutlined',一切正常,图标区域现在显示<PieChartOutlined />。我不知道为什么会这样,请帮我解决这个问题。

left-navigation.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>
    );
  }
}

menuList.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;

【问题讨论】:

    标签: reactjs antd ant-design-pro


    【解决方案1】:

    你传递的是'&lt;PieChartOutlined /&gt;'的字符串,需要直接传递组件。

    import { PieChartOutlined } from '@ant-design/icons';
    

    和:

          {
            title: 'Product Control',
            key: '/product',
            icon: <PieChartOutlined />,
          },
    

    如果您还没有安装 ant-design/icons,则需要安装:

    npm install --save @ant-design/icons

    【讨论】:

    • 是的,你是对的,还需要从'react'导入React;
    【解决方案2】:

    由于之前版本的性能原因,antd 团队正在应用 tree-shaking 来使用图标。更详细的可以查看https://ant.design/docs/react/migration-v4

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-14
      • 2019-10-22
      • 1970-01-01
      • 2021-02-08
      • 2019-06-24
      • 1970-01-01
      • 2020-04-11
      • 1970-01-01
      相关资源
      最近更新 更多