From this answer by Aditya Kresna Permana
For me, It's not working correctly because :image-url
in ParentComponent.vue not the same as props
in ChildComponentExample.vue
:image-url="require('~/assets/myimage.png')"
change :image-url
to :imageUrl
(same as in props
in ChildComponentExample.vue )
Result: :imageUrl="require('~/assets/myimage.png')"
Example ParentComponent.vue:
<template>
<div v-for="item in items" :key="item.id>
<ChildComponent
:imageURL="require(`~/assets/${item.imgURL}`)"
:title="item.title"
:descriptions="item.descriptions"
/>
</div>
</template>
<script>
import ChildComponent from '~/components/ChildComponentExample.vue'
export default {
components:{
ChildComponent,
},
data() {
return {
items: [
{
id: 1,
imgURL: '01-1.webp',
title: 'Title1',
descriptions: 'Lorem ipsum',
},
{
id: 2,
imgURL: '02-1.webp',
title: 'Title2',
descriptions: 'Lorem ipsum',
},
],
}
}
}
</script>
Example ChildComponentExample.vue:
<template>
<div>
<div>
<img :src="imageURL" alt="Alt text" />
<div>
<h3> {{ title }} </h3>
<div> {{ descriptions }} </div>
</div>
</div>
</div>
</template>
<script>
export default {
props: {
imageURL: {
type: String,
required: true,
default: '',
},
title: {
type: String,
required: true,
default: 'Title',
},
descriptions: {
type: String,
required: true,
default: 'No descriptions',
},
},
}
</script>