Vue.js data-bind style backgroundImage not working

后端 未结 9 1247
耶瑟儿~
耶瑟儿~ 2020-12-12 16:18

I\'m trying to bind the src of an image in an element, but it doesn\'t seem to work. I\'m getting an \"Invalid expression. Generated function body: { backgroundImage:{ url(

相关标签:
9条回答
  • 2020-12-12 16:48
    <div :style="{ backgroundImage: `url(${post.image})` }">
    

    there are multiple ways but i found template string easy and simple

    0 讨论(0)
  • 2020-12-12 16:53
    <div :style="{'background-image': 'url(' + require('./assets/media/img.jpg') + ')'}"></div>
    
    0 讨论(0)
  • 2020-12-12 16:55

    Binding background image style using a dynamic value from v-for loop could be done like this.

    <div v-for="i in items" :key="n" 
      :style="{backgroundImage: 'url('+require('./assets/cars/'+i.src+'.jpg')+')'}">
    </div>
    
    0 讨论(0)
提交回复
热议问题