1

I have got a vue.js component with a div.icon:

Vue.component('obj', {
  props: ['img', 'name'],
  template: '<div><div class="icon"></div> {{ name }}</div>'
})

While its easy to use {{ name }}, I do not know how to pass {{ img }}.

I have tried to add the following code, which does not work:

v-bind:style="{ backgroundImage: 'url(' + {{ img }} + ')' }"

How can I use the props: img to assign a background-image?

1 Answer 1

4

Use 'background-image' from this issue:

v-bind:style="{ 'background-image': 'url(' + img + ')' }"
Sign up to request clarification or add additional context in comments.

4 Comments

This causes an error: > Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead. For example, instead of <div style="{{ val }}">, use <div :style="val">.
thanks for the suggestion, but this does work neither. As the whole template: '' tag is wrapped inside the ' ' signs, the code creates a syntax error.
Escape them with \'
Thanks. Please add this important note to your answer for others to find it :)

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.