I am trying to load an image url into a component using props but it seems like require cannot accept any variable. However, if I give require a plain text as parameter, it works
This one gives the error
Cannot find module '../assets/logo.png' at webpackEmptyContext (eval at ./src/component
<template>
<div>
{{imagelink}}
<div style="width: 150px; height: 150px; background-color: red">
<img :src="imglink" alt="" height="150px" width="150px">
</div>
</div>
</template>
<script>
export default {
name: "ImageTest",
props:{
imagelink: String,
},
computed: {
imglink: function () {
// this.imagelink
const modulepatha = '../assets/logo.png'
return require(modulepatha)
}
}
}</script>
<style scoped>
</style>
This one works:
<template>
<div>
{{imagelink}}
<div style="width: 150px; height: 150px; background-color: red">
<img :src="imglink" alt="" height="150px" width="150px">
</div>
</div>
</template>
<script>
export default {
name: "ImageTest",
props:{
imagelink: String,
},
computed: {
imglink: function () {
// this.imagelink
const modulepatha = '../assets/logo.png'
return require('../assets/logo.png') //changed this
}
}
}</script>
<style scoped>
</style>
Notice that I only changed the value inside require to a plain text