How can I check if a Slot content is provided? #9393
-
I've encountered a situation where I need to determine if the Slot content is provided. In Vue2, I used the following code to conditionally render slot and wrapper if it existed: <!-- working -->
<div v-if="$slots.default">
<slot />
</div> However, after migrating to Vue3, when a slot is empty, <!--
not working and
$slots.default() returns
[
{
type: Fragment,
children: []
}
]
-->
<div v-if="$slots.default()">
<slot />
</div> This has made it impossible to check if the Slot content is provided by a simple null check or array length check. There are a private method used inside Vue3, but it is not exported and cannot be used. What is the correct and reliable way to check if the Slot content is provided? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
see #4733 |
Beta Was this translation helpful? Give feedback.
-
I just viewed #4733 and while there are some good examples in there, there isn't a totally agreed upon way of doing it. I have this same issue. Migrating a massive project to Vue 3 and there is extensive use of
Not having some mechanism to do the same seems like a real oversight. |
Beta Was this translation helpful? Give feedback.
-
hello my friend , this will help you . this is the sorce code - |
Beta Was this translation helpful? Give feedback.
see #4733