I want to display a picture from assets folder based on some property value. I have created a method with a switch. The picture was not found. After some googling I realized that this is caused by webpack and I shall use require. But I still encounter strange errors.
This works:
<img src="@/assets/happy.png" class="pr-2" align="middle">
Dynamic:
<img :src="this.votedPicture" class="pr-2" align="middle">
votedPicture() {
switch (this.$store.getters.POLL.my_vote) {
case 'neutral':
return require('@/assets/happy.png');
default:
return require('@/assets/angry.png');
}
},
},
This fails with Unexpected require().
When I move the require to img:
<img :src="require(this.votedPicture)" class="pr-2" align="middle">
there is an error in console:
vue.runtime.esm.js:1888 Error: Cannot find module '@/assets/happy.png'
at webpackEmptyContext (organisms sync:2)
at Proxy.render (CompletePoll.vue?4b27:31)
at VueComponent.Vue._render (vue.runtime.esm.js:3548)
So what is the proper way?
:src="votedPicture", nothisprefix required