Javascript 如何在ReactJs中为backgroundImage编写正确的语法?
我试图在map()函数中定义a的backgroundImage,但我不明白添加一个的正确语法是什么: 我有一个名为value的变量,用于存储文件名,我想在我的backgroundImage中插入uploads/,因为我希望backgroundImage在其中获取图像 我试过:Javascript 如何在ReactJs中为backgroundImage编写正确的语法?,javascript,reactjs,Javascript,Reactjs,我试图在map()函数中定义a的backgroundImage,但我不明白添加一个的正确语法是什么: 我有一个名为value的变量,用于存储文件名,我想在我的backgroundImage中插入uploads/,因为我希望backgroundImage在其中获取图像 我试过: <div className="image-show" key={index} style={{backgroundImage: URL({'uploads/' + value})}}> 但它返回以下错误
<div className="image-show" key={index} style={{backgroundImage: URL({'uploads/' + value})}}>
但它返回以下错误:
提前感谢您可以尝试:
style={{backgroundImage: "url(uploads/" + value + ")" }}
//OR
style={{backgroundImage: `url(uploads/${value})` }}
替换:
{'uploads/'+value}
以下是:
{ `uploads/${value}`}
试试这个:
<div className="image-show" key={index} style={{backgroundImage: "url(uploads/" + value + ")" }}>
在组件中传递的CSS样式字段必须具有数字或字符串形式的值 而不是
style={{backgroundImage:URL({uploads/12345}}}
React期望style={{{backgroundImage:“url(uploads/12345)”}
我希望这会有所帮助非常感谢,我尝试了第二个选项,效果很好@安东尼奥利马很高兴知道这一点,它起了作用:)
backgroundImage: "url(" + background_image + ")"
--- where background_image is a string(path of your image)