如何将svg导入antd->图标组件 [英] How to import svg into antd -> Icon component
问题描述
嘿,我正在尝试将自己的svg导入antd->图标组件 就像在文档中一样,但我遇到了例外
Hey I am trying to import my own svg into antd -> Icon component like in the documentation but i got an exception
InvalidCharacterError:无法在上执行'createElement' '文档':提供的标签名称 ('/static/media/archive-solid.3c305173.svg')不是有效的名称.
InvalidCharacterError: Failed to execute 'createElement' on 'Document': The tag name provided ('/static/media/archive-solid.3c305173.svg') is not a valid name.
我正在使用create react app 2.1.1和antd版本3.10.3 我不想执行创建反应应用程序弹出并且无法访问webpack 有任何想法吗. 该代码:
Im using create react app 2.1.1 and antd version 3.10.3 I would not want to do the create react app eject and i dont access to webpack any ideas. that the code:
import React, { Component } from "react";
import { Layout, Menu, Icon } from "antd";
import ArchiveSVG from "../../../img/icons/archive-solid.svg";
const { Sider } = Layout;
class SideBar extends Component {
state = {
collapsed: false
};
onCollapse = collapsed => {
this.setState({ collapsed });
};
render() {
return (
<Sider
collapsible
collapsed={this.state.collapsed}
onCollapse={this.onCollapse}
>
<div className={styles.logo} />
<Menu theme="dark" defaultSelectedKeys={["1"]} mode="inline">
<Menu.Item key="4">
<Icon component={ArchiveSVG} />
<span>Products</span>
</Menu.Item>
</Menu>
</Sider>
);
}
}
推荐答案
此方法对我有用
import ArchiveSVG from "../../../img/icons/archive-solid.svg";
const archive = () => (
<img src={ArchiveSVG} />
);
然后像这样使用
<Icon component={archive} />
快乐编码:-)
这篇关于如何将svg导入antd->图标组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!