I'm trying to generate dynamic image paths, but I'm missing something. For one reason or another, the method isn't executing on the src
attribute in the img
tags.
Here is the HTML:
<div id="app">
<component v-bind:fruits="fruits" is="fruits-list"></component>
</div>
<template id="fruits-template">
<div>
<ul>
<li v-for="fruit in fruits">
{{ fruit }}
<img src="imgName(fruit)" />
</li>
</ul>
</div>
</template>
And here is the relevant JS:
Vue.component('fruits-list', {
template:'#fruits-template',
props: ['fruits'],
methods: {
imgName: function(fruit){
var imgPath = 'assets/images/' + fruit + '.jpg';
return imgPath;
}
}
})
new Vue({
el: '#app',
data() {
return {
fruits: ['apple','pear','strawberry', 'peach']
}
}
})
I also did try binding the src
to the img
tag like :
<img :src="getImageUrl(fruit)" />
But that causes nothing to render at all. Is it better here to use a method or computed properties in an instance like this?