简体   繁体   English

显示图片 <img src='path' /> 在React中有一些问题

[英]Show images <img src='path' /> in React, has some issues

I'm creating a template front-page in React and I need to show many local-dummy images . 我正在React中创建一个模板首页,我需要显示许多本地虚拟图像

Inside my <Main /> component I import other components which pass the image path as a parameter, like so: 在我的<Main />组件内部,我导入了其他组件,这些组件将图像路径作为参数传递,如下所示:

  <Container
    type="image"
    source=...
    message=""
    link="" />

As I have searched and read on different answers, so far in React we can't just use <img src="relative image path" /> , I don't know exactly what is the issue, cache thing / render thing etc. 由于我已经搜索并阅读了不同的答案,因此到目前为止,在React中我们不能仅仅使用<img src="relative image path" /> ,我不知道到底是什么问题,缓存对象/渲染对象等。

What I've done so far in order to load the images on page: 到目前为止,为了加载页面上的图像,我做了什么:
1. First importing them on the top: import img1 from '../assets/img/hd1080.png' 1.首先将它们import img1 from '../assets/img/hd1080.png'顶部: import img1 from '../assets/img/hd1080.png'
2. Pass the image to the component like so: 2.将图像传递到组件,如下所示:

<Container
    type="image"
    source={img1}
    message=""
    link="" />
  1. Inside , I pass it to the img like so: <img src={img1} /> 在内部,我将其传递给img,如下所示: <img src={img1} />

Just wondering if there is a faster way, that does not need to first importing the images and then using them, src="relative path" 只是想知道是否有更快的方法,不需要先导入图像然后使用它们,即src="relative path"

In React you can also use the relative path of an image as well. 在React中,您也可以使用图像的相对路径。

However, considering your question, you probably have Webpack compiling and bundling your assets. 但是,考虑到您的问题,您可能需要Webpack来编译和捆绑资产。 "The Webpack Way" prefers to have the compilation of everything handled by Webpack. “ Webpack方式”更喜欢对Webpack处理的所有内容进行编译。 This has some merits such as being able to optimize and inline images via Webpack plugins. 这具有一些优点,例如能够通过Webpack插件优化和内嵌图像。

Another approach would be to compile your JS & CSS with Webpack but copy the images as-is to the "compiled folder". 另一种方法是使用Webpack编译JS和CSS,然后将图像原样复制到“已编译的文件夹”中。

Webpack's development server lets you do this if you want, and sometimes it's easier and makes more sense than having the JS files import the images. Webpack的开发服务器允许您根据需要执行此操作,有时它比让JS文件导入图像更容易,更有意义。

If your only goal is simplicity, then it's simpler to import - you don't need another "file copier" script for your file compilation, and Webpack can compress or inline your images. 如果您的唯一目标是简单,那么导入就更简单-您不需要其他“文件复印机”脚本来进行文件编译,并且Webpack可以压缩或内联图像。

However, if importing every image becomes tedious to manage, consider making a simple file copy rule (with Makefiles for example), copying the files to the dist folder and simply writing <Container source="/images/x.png"> . 但是,如果导入每个图像都变得难以管理,请考虑制定一个简单的文件复制规则(例如,使用Makefiles),将文件复制到dist文件夹,然后简单地编写<Container source="/images/x.png">

If you are dealing with multiple files then i would suggest you to make a separate file in and import all your images their. 如果您要处理多个文件,那么我建议您制作一个单独的文件并导入其所有图像。 For exp : 对于exp:

module.exports = {
    Slider_Arrow_Icon: {
       back_Arrow_Icon: require('../images/arrow-slide-back')
    }

} }

And then import this file and you can use easily. 然后导入此文件,您可以轻松使用。 If any query lmk. 如果有任何查询lmk。

Hope this find useful. 希望这对您有用。

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

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