[英]Image path not working in React as I tried to use use Image path inside an object which is also inside of an array
I wanted to use image path inside an object as a value.我想使用 object 中的图像路径作为值。 This object is also inside of an array.
这个 object 也在一个数组里面。 But the image is not showing up.
但是图像没有显示出来。 Can anyone explain why?
谁能解释为什么? My code:
我的代码:
const Data =[
{
imgsrc:"{require('../src/img/2.jpg')}",
title:'this is me',
sname:'Dark',
link:'https://www.google.com'
},
{
imgsrc:"{require('../src/img/1.jpg')}",
title:'this is me',
sname:'Dark',
link:'https://www.google.com'
}
];
export default Data;
Because this are strings, not proper import statements?因为这是字符串,而不是正确的导入语句? Webpack or whatever tool you are using is unable to find this imports and interpret them correctly.
Webpack 或您使用的任何工具都无法找到此导入并正确解释它们。
Try something like this:尝试这样的事情:
const image1 = require('../src/img/1.jpg')
const image2 = require('../src/img/1.jpg')
const Data =[
{
imgsrc:image1,
title:'this is me',
sname:'Dark',
link:'https://www.google.com'
},
{
imgsrc:image2,
title:'this is me',
sname:'Dark',
link:'https://www.google.com'
}`enter code here`
];
You cannot use require in the way you are proposing.您不能以您提议的方式使用 require 。 Well, actually of course there is a way someone know how it can work, but I would highly recommend to not do it.
好吧,实际上当然有人知道它是如何工作的,但我强烈建议不要这样做。
Consider this option:考虑这个选项:
const image2 = require('../src/img/2.jpg');
const Data =[
{
imgsrc:image2,
title:'this is me',
sname:'Dark',
link:'https://www.google.com'
},
];
export default Data;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.