How can I use 'img src' in Vue.js?

Javascriptvue.js

Javascript Problem Overview


I have this in my Vue.js template:

<img src="/media/avatars/{{joke.avatar}}" alt="">

It is inside a loop that renders jokes. Other fields are rendered fine, but for the image I get this error in the console:

> - src="/media/avatars/{{joke.avatar}}": Interpolation inside attributes has been removed. Use v-bind or the colon shorthand > instead. For example, instead of

, use
:id="val">.

I have also use v-bind:src="..., but I get invalid expression error.

How can I fix this?

Javascript Solutions


Solution 1 - Javascript

Try this:

<img v-bind:src="'/media/avatars/' + joke.avatar" /> 

Don't forget single quote around your path string. Also in your data, check you have a correctly defined image variable.

joke: {
  avatar: 'image.jpg'
}

A working demo is here: http://jsbin.com/pivecunode/1/edit?html,js,output

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKarlomView Question on Stackoverflow
Solution 1 - JavascriptBadis MerabetView Answer on Stackoverflow