Vue.js data-bind style backgroundImage not working

后端 未结 9 1246
耶瑟儿~
耶瑟儿~ 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:28

    The issue is that the value for backgroundImage needs to be a string like this:

    <div class="circular" v-bind:style="{ backgroundImage: 'url(' + image + ')' }"></div>
    

    Here's a simplified fiddle that's working: https://jsfiddle.net/89af0se9/1/

    Re: the comment below about kebab-case, this is how you can do that:

    <div class="circular" v-bind:style="{ 'background-image': 'url(' + image + ')' }"></div>
    

    In other words, the value for v-bind:style is just a plain Javascript object and follows the same rules.

    UPDATE: One other note about why you may have trouble getting this to work.

    You should make sure your image value is quoted so that the end resulting string is:

    url('some/url/path/to/image.jpeg')
    

    Otherwise, if your image URL has special characters in it (such as whitespace or parentheses) the browser may not apply it properly. In Javascript, the assignment would look like:

    this.image = "'some/url/path/to/image.jpeg'"
    

    or

    this.image = "'" + myUrl + "'"
    

    Technically, this could be done in the template, but the escaping required to keep it valid HTML isn't worth it.

    More info here: Is quoting the value of url() really necessary?

    0 讨论(0)
  • 2020-12-12 16:32

    I tried @david answer, and it didn't fix my issue. after a lot of hassle, I made a method and return the image with style string.

    HTML Code

    <div v-for="slide in loadSliderImages" :key="slide.id">
        <div v-else :style="bannerBgImage(slide.banner)"></div>
    </div>
    

    Method

    bannerBgImage(image){
        return 'background-image: url("' + image + '")';
    },
    
    0 讨论(0)
  • 2020-12-12 16:38

    For single repeated component this technic work for me

    <div class="img-section" :style=img_section_style >
    
    computed: {
                img_section_style: function(){
                    var bgImg= this.post_data.fet_img
                    return {
                        "color": "red",
                        "border" : "5px solid ",
                        "background": 'url('+bgImg+')'
                    }
                },
    }
    
    0 讨论(0)
  • 2020-12-12 16:42

    I experienced an issue where background images with spaces in the filename where causing the style to not be applied. To correct this I had to ensure the string path was encapsulated in single quotes.

    Note the escaped \' in my example below.

    <div :style="{
        height: '100px',
        backgroundColor: '#323232',
        backgroundImage: 'url(\'' + event.image + '\')',
        backgroundPosition: 'center center',
        backgroundSize: 'cover'
        }">
    </div>
    
    0 讨论(0)
  • 2020-12-12 16:45

    The accepted answer didn't seem to solve the problem for me, but this did

    Ensure your backgroundImage declarations are wrapped in url( and quotes so the style works correctly, no matter the file name.

    ES2015 Style:

    <div :style="{ backgroundImage: `url('${image}')` }"></div>
    

    Or without ES2015:

    <div :style="{ backgroundImage: 'url(\'' + image + '\')' }"></div>
    

    Source: vuejs/vue-loader issue #646

    0 讨论(0)
  • 2020-12-12 16:47

    Another solution:

    <template>
      <div :style="cssProps"></div>
    </template>
    
    <script>
      export default {
        data() {
          return {
            cssProps: {
              backgroundImage: `url(${require('@/assets/path/to/your/img.jpg')})`
            }
          }
        }
      }
    </script>
    

    What makes this solution more convenient? Firstly, it's cleaner. And then, if you're using Vue CLI (I assume you do), you can load it with webpack.

    Note: don't forget that require() is always relative to the current file's path.

    0 讨论(0)
提交回复
热议问题