[Bug] When `slot` is used with `v-if`, the slot content is not displayed as expected
Vue version
^3.2.45
Link to minimal reproduction
Steps to reproduce
- open the reproduction link
- click the toggle button
- observe whether the slot content is toggled.
What is expected?
- The provided content should be displayed when
show
istrue
. - The fallback content should be displayed when
show
isfalse
.
What is actually happening?
The slot content is not toggled.
System Info
No response
Any additional comments?
No response
a workaround
<Container :show-children="!show">
<div v-if="show" :key="1"> // add a key
Provided Content
</div>
</Container>
a workaround
<Container :show-children="!show"> <div v-if="show" :key="1"> // add a key Provided Content </div> </Container>
This method can temporarily solve this problem, but if the component comes from an external library, we may need to find ways to avoid key
repetition.
this is because of the slot children and the fallback children both as blocks but with empty dynamic children.
@nooooooom
<template>
<slot>
<div> // remove v-if also works.
Fallback Content
</div>
</slot>
</template>
I usually use $slots.default
to determine if need to display the fallback content
<slot v-if="$slots.default">
<div>
Fallback Content
</div>
</slot>
this is because of the slot children and the fallback children both as blocks but with empty dynamic children.
@nooooooom
<template> <slot> <div> // remove v-if also works. Fallback Content </div> </slot> </template>
I usually use
$slots.default
to determine if need to display the fallback content<slot v-if="$slots.default"> <div> Fallback Content </div> </slot>
Thank you for your suggestion. Currently, I was able to solve my problem this way.
But this may still be something Vue needs to address, as it doesn't align with the usage as stated in the documentation.