使用JSX在React中显示图像而不导入 [英] Display images in React using JSX without import

查看:87
本文介绍了使用JSX在React中显示图像而不导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的问题是img标签。当涉及单个时,
以下代码加载图像:

The problem i face is with img tag. When a single is concerned, The below code loads the image:

import image1 from './images/image1.jpg;
<img src={image1} />

但以下代码未加载:

 <img src={'./images/image1.jpg'}/>
            or
  <img src='./images/image1.jpg'/>

我需要循环浏览json,类似于[{'img':'。/ images / image1 .jpg','name':'AAA'},{'img':'./ images / image2.jpg','name':'BBB'}]并将每个显示为图片,名称为footer。循环很好,但图像无法加载。实际上我自己无法导入要添加的每个图像。我现在不使用除JSX以外的任何东西。请支持。

I need to loop through json , something like [{'img':'./images/image1.jpg','name':'AAA'},{'img':'./images/image2.jpg','name':'BBB'}] and display each of them as image with name as footer. Looping is fine but images doesn't load.It is not actually possible for myself to import every images to add. I don't use anything other than JSX as of now.Please favour.

推荐答案

你需要像这样要求文件:

You need to require the file like so:

<img src={ require('./images/image1.jpg') } />

这篇关于使用JSX在React中显示图像而不导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆