VueJS v-bind:背景图像的样式:url()

新手上路,请多包涵

根据 VueJS 文档:

<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>

我尝试了几种模式:

 <div v-bind:style="{ background-image: url(item.img), }"></div>

<div v-bind:style="{ 'background-image': url('item.img'), }"></div>

<div v-bind:style='{ backgroundImage: "url(item.img)", }'></div>

但结果对 HTML style 属性无效。

有任何想法吗?

原文由 Gus 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 565
1 个回答

尝试其他模式后,这是有效的模式:

<div v-bind:style='{ backgroundImage: "url(" + item.img + ")", }'></div>

结果是:

<div style='{ background-image: url("/img/path/img.jpg"), }'></div>

原文由 Gus 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题