React - import multiple svgs in a single file

一世执手 提交于 2021-02-10 22:40:25

问题


I have multiple svg files and I want to import and export all of them in a single file:

icons.js

import IconVideoOn from '../../assets/img/icons/video-on.svg';
import IconVideoOff from '../../assets/img/icons/video-off.svg';
import IconMicOn from '../../assets/img/icons/mic-on.svg';
import IconMicOff from '../../assets/img/icons/mic-off.svg';
.
.
.

export default {
    IconVideoOn,
    IconVideoOff,
    IconMicOn,
    IconMicOff
};

then I'm importing them like this

MyComponent.jsx

import { IconVideoOn } from '../../components/Icons/icons';

But I'm getting this error on my component, can't figure out why:

./src/components/MyComponent/MyComponent.jsx Attempted import error: 'IconVideoOn' is not exported from '../../components/Icons/icons'.


回答1:


You cant export default multiple value type thing.

Either do this:

export {
    IconVideoOn,
    IconVideoOff,
    IconMicOn,
    IconMicOff
};

then your existing import works

Or do this:

const MySVG = {
    IconVideoOn,
    IconVideoOff,
    IconMicOn,
    IconMicOff
};

export default MySVG;
 

Then import like this:

import MySVG from '../../components/Icons/icons';

and use like this:

<img src={MySVG.IconVideoOn} alt=""/>


来源:https://stackoverflow.com/questions/63647648/react-import-multiple-svgs-in-a-single-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!