I am encountering an issue in my NuxtJS project where a component is not displaying an image correctly. Despite passing the image path directly to :src="imageAddress"
, it does not resolve nor throw an error. I attempted using the path inside require()
to properly resolve it, but now I am faced with a Nuxt error: "Cannot find module '~/assets/icons/crown.png'". Strangely, when I placed an img
element directly in index.vue
, it displayed fine. Any insights on why this might be happening? Here's how my code is structured:
pages/index.vue
<template>
<ChildComponent image-address="~/assets/icons/crown.png" />
</template>
___________________________________________________________________
components/ChildComponent.vue
<template>
<img v-if="imageAddress.length" :src="require(imageAddress)">
</template>
<script>
export default {
name: 'ChildComponent',
props: {
imageAddress: {
type: String,
required: true,
default: ''
}
}
}
</script>