简体   繁体   English

从另一个方向导入整个 SVG 图像文件夹

[英]Importing whole SVG images folder from another direction

I want to import whole folder with my SVG icons from another direction.我想用我的 SVG 图标从另一个方向导入整个文件夹。 For now i know how to import a single file one by one.现在我知道如何一个一个地导入一个文件。 Want to import the whole folder, to make code cleaner and to faster implementation.想要导入整个文件夹,以使代码更干净并更快地实现。

Tryed to do this like this import Icon from '../../icons'尝试这样做import Icon from '../../icons'

but that's not the right way i guess.但这不是我猜的正确方式。

import './WeatherBox.css'
import Icon from '../../icons'

const dateBuilder = (d) => {
    let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

    let day = days[d.getDay()];
    let date = d.getDate();
    let month = months[d.getMonth()];
    let year = d.getFullYear();

    return `${day} ${date} ${month} ${year}`
}

const WeatherBox = ({result}) => {
    return (
        <div className="locationDate">
            <span>{result.location}</span>
            <span>{dateBuilder(new Date())}</span>
            <span>{result.temp}°C</span>
            <img src={Icon.clear} alt=""/>            
        </div>
    )
}

export default WeatherBox;


If the Icons are in jsx format the way you do is correct.如果图标是 jsx 格式,那么您的做法是正确的。 You can import them like any other component in jsx or tsx format您可以像其他任何组件一样以 jsx 或 tsx 格式导入它们

Since you're using React, you're most likely using webpack as well.由于您使用的是 React,因此您很可能也使用 webpack。 You must reference all icons in the code, or they won't be available later.您必须引用代码中的所有图标,否则它们以后将不可用。 You could put them in a static directory and then fetch them that way.您可以将它们放在 static 目录中,然后以这种方式获取它们。 Meaning you send a HTTP GET request to /static/directory/${icon}.svg , this way you can load them without having to reference them.这意味着您向/static/directory/${icon}.svg发送 HTTP GET 请求,这样您就可以加载它们而无需引用它们。

Another solution is to mention them in the code, for example you can create an index.js file inside the icons directory with the following code:另一种解决方案是在代码中提及它们,例如您可以使用以下代码在icons目录中创建一个 index.js 文件:

import clear from './clear.svg';

export const Icons = {
  clear,
}

This way you can do the following inside your code:这样,您可以在代码中执行以下操作:

import { Icons } from '../../icons';
Icons.clear // URL to the svg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM