鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ vue中如何給backgroundImage中的url賦值?

vue中如何給backgroundImage中的url賦值?

<div class="img2" :style="{backgroundImage:'url(http://xx.com:8091//upload/17.jpg)'}"></div>


這里http://xx.com:8091//upload/17.jpg是常量,所以能正常顯示

如果換成變量http_url='http://xx.com:8091//upload/17.jpg';

請問如何把http_url傳進去?


ps:網(wǎng)上提供的解決辦法:`<div class="img2" :style="{backgroundImage:'url('+http_url+')'}">
</div>`

貌似不行


。。。。。。。。。。。。。補充。。。。。。。。。。。。。。。。。。。。。。
我在data里已經(jīng)返回了一個worklist,下面item就是worklist遍歷的子項。
<div v-for="src of item.images" class="img2" :style="{backgroundImage:'url('+src+')'}"></div>
所以只能在頁面渲染部分解決這個問題。
現(xiàn)在出現(xiàn)的問題是:`<div v-for="src of item.images">{{src}}</div>`打印出來的結(jié)果是好的,比如:`http://xx/uploads/2.jpg`。
但是`<div v-for="src of item.images" class="img2" :style="{backgroundImage:'url('+src+')'}"></div>`就會有問題,錯誤信息顯示傳給src參數(shù)是服務(wù)器上的絕對路徑,例如:`D:/www/2.jpg`。

回答
編輯回答
喜歡你

不能把這個style對象直接扔進計算屬性么,然后動態(tài)獲取這個url拼接后返回,直接寫在模版里面多麻煩,維護起來更麻煩

2018年5月7日 21:58
編輯回答
初念

字符串模板,${}

2017年10月10日 12:35
編輯回答
半心人

正常字符串處理不行嗎?
'url('+imageUrl+')',把imageUrl放到組件的data里。


不明白什么是“貌似不行”,是報錯了還是單單無效?寫了個測試demo JSfiddle測試

補充的內(nèi)容沒貼代碼,也沒具體報錯信息,想象不出。

2017年8月19日 20:29