动态引入 Ant Design 图标

514 阅读1分钟

React 中动态引入 Ant Design 图标

在 React 项目中,你可以通过 React.createElement 方法将图标字符串转换为图标组件。首先,你需要引入所有的 Ant Design 图标:

import * as Icons from '@ant-design/icons';

const Iconfont = (props) => {
  const { icon } = props;
  return React.createElement(Icons[icon]);
}

<Iconfont icon="HomeOutlined" />

Vue 中动态引入 Ant Design 图标

在 Vue 项目中,你可以通过 createVNode 方法将图标字符串转换为图标组件。首先,你需要引入所有的 Ant Design 图标:

import * as Icons from '@ant-design/icons-vue';

const Icon = (props) => {
  const { icon } = props;
  return createVNode(Icons[icon as keyof typeof Icons]);
}

<Icon icon="HomeOutlined" />