I have a vue playground.
The container component needs to be able to control the display status of an unknown number of children in the default slot.
I assume the child components will need to hold a property or have some way of identifying themselves to their container.
How can the container change the display property of one of the components in the default slot?
App.vue
<script setup>
import Container from './Container.vue'
import ChildA from './ChildA.vue'
import ChildB from './ChildB.vue'
</script>
<template>
<Container>
<ChildA />
<ChildB />
</Container>
</template>
Container.vue
<script setup>
import { useSlots, useAttrs, onMounted} from 'vue'
const slots = useSlots()
function logSlots(where) {
console.log( `${where} slots`, slots )
const children = slots.default()
console.log( `${where} slots children`, children.length, children )
}
logSlots("setup")
function changeDisplay(whichChild, show) {
console.log( "change display", whichChild, show)
// how can I know I am accessing child a?
// what goes here?
}
onMounted( () => {
logSlots("onMounted")
})
</script>
<template>
<button @click="logSlots('button')">log slots</button>
<button @click="changeDisplay('child a', false)">Hide Child A</button>
<button @click="changeDisplay('child a', true)">Show Child A</button>
<slot />
</template>
ChildA.vue
<script setup>
</script>
<template>
<div>
ChildA
</div>
</template>
ChildB.vue
<script setup>
</script>
<template>
<div>
ChildB
</div>
</template>