My Vue component contains some images. I want to do lazy-loading later, so I need to set the src of the images to a small image, first.
<template>
<div v-for="item in portfolioItems">
<a href="#{{ item.id }}">
<img
data-original="{{ item.img }}"
v-bind:src="/static/img/clear.gif"
class="lazy" alt="">
</a>
</div>
</template>
Gives me a bunch of errors, like:
[Vue warn]: Invalid expression. Generated function body: /scope.static/scope.img/scope.clear.gif vue.common.js:1014[Vue
[Vue warn]: Error when evaluating expression "/static/img/clear.gif": TypeError: Cannot read property 'call' of undefined (found in component: )
webpack.config.js: module.exports = { // ... build: { assetsPublicPath: '/', assetsSubDirectory: 'static' } }
This solution is for Vue-2 users:
vue-2
if you don't like to keep your files instatic
folder (relevant info), orvue-2
&vue-cli-3
if you don't like to keep your files inpublic
folder (static
folder is renamed topublic
):The simple solution is :)
If you like to keep your static images in
static/assets/img
orpublic/assets/img
folder, then just do:If you want to bind a string to the
src
attribute, you should wrap it on single quotes:IMO you do not need to bind a string, you could use the simple way:
Check an example about the image preload here: http://codepen.io/pespantelis/pen/RWVZxL
And on the template part:
See it in action here
@Pantelis answer somehow steered me to a solution for a similar misunderstanding. A message board project I'm working on needs to show an optional image. I was having fits trying to get the src=imagefile to concatenate a fixed path and variable filename string until I saw the quirky use of "''" quotes :-)