Hi! This is my first post on this forum and I would like to know if there are any differences between these 2 ways of passing down a prop from parent → children → more children. Each component can have an arbitrary number of children.
Is there any difference between these 2 methods of passing a certain object down from the parent assuming I will always pass down a context? I am leaning towards the 2nd approach with the <component>
tag and useSlots()
.
Thank you for the help and apologies if this has been asked before!
Using Slots
App.vue
<template>
<Parent :context="context" v-slot="slotProp">
<Child :context="slotProp.context" v-slot="slotProp">
<DeeperChild :context="slotProp.context" />
</Child>
</Parent>
</template>
Parent.vue (same for the child compnents)
<template>
<slot :context="context" />
</template>
Using the component tag and useSlots
App.vue
<template>
<Parent :context="context">
<Child>
<DeeperChild/>
</Child>
</Parent>
</template>
Parent.vue (same for the child components)
<script setup>
import { useSlots } from 'vue';
const props = defineProps({
context: {
type: Object,
},
})
})
const slots = useSlots();
</script>
<template>
<component
v-if="slots?.default"
v-for="(component, i) in slots.default()"
:key="i"
:is="component"
:context="doSomeChange(context)"
/>
</template>