如何在ReactJs中为backgroundImage编写正确的语法?

时间:2020-06-11 11:54:42

标签: javascript reactjs

我正在尝试在map()函数中定义一个backgroundImage,但是我不明白添加一个正确的语法是什么:

我有一个名为 value 的变量,用于存储文件的名称,并且我想在我的backgroundImage中插入 uploads / ,因为这是我希望backgroundImage提取其中的文件夹图片。

我尝试过:

<div className="image-show" key={index} style={{backgroundImage: URL({'uploads/' + value})}}>

但是它返回以下错误:

Error

预先感谢

4 个答案:

答案 0 :(得分:1)

您可以尝试:

style={{backgroundImage: "url(uploads/" + value + ")" }}

//OR

style={{backgroundImage: `url(uploads/${value})` }}

答案 1 :(得分:0)

替换: {'uploads/' + value}

具有以下内容:

{ `uploads/${value}`}

答案 2 :(得分:0)

尝试一下:

<div className="image-show" key={index} style={{backgroundImage: "url(uploads/" + value + ")" }}>

一般语法是:

backgroundImage: "url(" + background_image + ")"   

--- where background_image is a string(path of  your image)

答案 3 :(得分:0)

您传入组件的CSS样式字段必须具有数字或字符串值。

代替style={ {backgroundImage: URL({uploads/12345})} }

反应期望style={{backgroundImage: "url(uploads/12345)" }}

我希望对您有帮助