4

Hi I am really new to vuejs. I try to import a picture then call this picture in the methods.

import image from '@/assets/svg/xxx.svg'

then in the data

data () {
        return {
            image: image
        }
    },

and try to use it

li.style.backgroundImage = "url('this.image')";

But the image is not showing, and image link show as following

http://127.0.0.1:8080/order/detail/29/this.image

I really not sure where i get wrong...Please help...

3
  • 1
    You should simple use the path to the image in your js. That would be li.style.backgroundImage = "url('/static/assets/svg/xxx.svg')" Commented Mar 20, 2018 at 10:03
  • unquote to parameter: "url(this.image)" Commented Mar 20, 2018 at 11:39
  • 1
    If I use "url('/static/assets/svg/xxx.svg')" It show the link with order/detail/static/assets/svg/xxx.svg Commented Mar 20, 2018 at 13:44

3 Answers 3

6

A another way to doing it using computed property on style attribute :

computed : {
    liStyle(){
        return "background-image : url('" + require('@/assets/svg/xxx.svg') + "')";
    }
}

Template :

<li :style="liStyle"></li>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot !!! This is working for me...May i ask you why there got "+" ??? like " + require('@/assets/svg/xxx.svg') + "
Concatenation, allowing webpack to compute le path to the resource (via require function).
3

I think the better (and scalable) solution it's to have a class which sets the styles you want, and add it to the elements, if it have to be dinamically, just add it with :class on some condition.

note: i used an external image path, but it's the same with your local path. And use require() to import images and html

new Vue({
  el: '#example'
})
.li-with-bg-image {
  background-image: url('http://via.placeholder.com/350x150')
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>

<div id="example">
  <ul>
    <li :class="{'li-with-bg-image': true}"></li>
    <li></li>
    <li class="li-with-bg-image"></li>
  </ul>
</div>

Another options

Using inline styles (using v-bind:style)

new Vue({
  el: '#example'
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>

<div id="example">
  <ul>
    <li :style="`background-image: url('http://via.placeholder.com/350x150')`"></li>
  </ul>
</div>

Using data section or a computed property binded to style

new Vue({
  el: '#example',
  data: {
    myStyles: `background-image: url('http://via.placeholder.com/350x150')`
  },
  computed: {
    anotherStyles () { 
      return `background-image: url('http://via.placeholder.com/300x150')`
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>

<div id="example">
  <ul>
    <li :style="myStyles"></li>
    <li></li>
    <li :style="anotherStyles"></li>
  </ul>
</div>

Resources:

Class and style bindings Docs

2 Comments

Thanks your help! but my problem is to find the image url. It always to add extra link..For example...if i set image url = @/assets/svg/xxx.svg, Then showing on the page url = 127.0.0.1:8080/order/detail/xxx, then when it render, the image url will be 127.0.0.1:8080/order/detail/assets/svg/xxx.svg..which is the page link + image link...of course, it won't show the picture because the picture is not in that folder. I need call the picture in vue methods, because I want to check the uploaded file type then update the different thumb icon
user require() to import it
1

I'm pretty sure you can't import an image via the import statement, only modules (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import).

I would recommend reading up on the Vue-Webpack-template and look at the static assets section.

In short you could use Webpack (or Gulp) to move your assets to a fixed location and then use links.

1 Comment

Thanks a lot your help..I am sure that the current project is already using webpack...So I think this is the reason the picture rending with page_url + image_url...I will read the static assets..I think it will help me to understand... But i solve the problem by using computed with liStyling...then call liStyling in the methods...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.