如何在react js中动态提供Image src? [英] How to give Image src dynamically in react js?
问题描述
我试图在 src 中动态地给出图像名称.我想使用带有路径的变量动态设置图像名称.但我无法正确设置 src.我在 stackoverflow 上尝试了解决方案,但没有任何效果.
I am trying to give image name in src dynamically. I want to set image name dynamically using variable with path. but I am not able to set src correctly. I tried solutions on stackoverflow but nothing is working.
我试图给出这样的路径
<img src={`../img/${img.code}.jpg`}></img>
<img src={'../img/' + img.code + '.jpg'}></img>
<img src={'../img/{img.code}.jpg'}></img>
我的图片保存在 src/img 路径如果我给出这样的路径
my images are saved in src/img path if i give path like this
<img src={require('../img/nokia.jpg')}/>
图像显示
我知道之前有人问过这个问题,但对我没有任何帮助.请帮我如何设置图片路径?
I know this question is asked before but nothing is working for me. Please help me how can I set image path?
推荐答案
如果你不想需要图片,那么你必须把你所有的图片放到 public 文件夹中,然后
if you dont want to require the image then you have to put all your images into public folder and then
<img src={`../img/${img.code}.jpg`}></img>
这个方法行得通.
这篇关于如何在react js中动态提供Image src?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!