You can check if a slot is empty, for example, only to render it if it is available or has content.
Check if a slot is empty
To check if a slot is empty, you can use $slots
, an object representing the slots passed by the parent component.
Each slot is exposed on $slots
as a function that returns an array of vnodes
under the key corresponding to that slot's name. The default slot is exposed as $slots.default
.
If a slot is a scoped slot, arguments passed to the slot functions are available to the slot as its slot props.
In the following example, the footer is only rendered if the slot with the name footer
is present:
<template>
<footer v-if="$slots.footer">
<h3>My Footer Heading</h3>
<slot name="footer" />
</footer>
</template>
<template>
<footer v-if="showFooter">
<h3>My Footer Heading</h3>
<slot name="footer" />
</footer>
</template>
<script>
export default {
data() {
return {
showFooter: false,
}
},
created() {
this.setShowSlots()
},
beforeUpdate() {
this.setShowSlots()
},
methods: {
setShowSlots() {
this.showFooter = this.$slots.footer?.[0]
},
},
}
</script>
useSlots composable
Usage of slots inside <script setup>
should be relatively rare since you can directly access them as $slots
in the template. In the rare case where you need them, you can use the useSlots
composable:
<template>
<footer v-if="showFooter">
<h3>My Footer Heading</h3>
<slot name="footer" />
</footer>
</template>
<script setup>
import { useSlots } from 'vue'
const slots = useSlots()
const showFooter = () => {
return !!slots.footer
}
</script>
useSlots
is a runtime function that returns the equivalent of setupContext.slots
. You can use it in normal Composition API functions as well.Check if a slot has content
In some cases, you probably want to check if the slot is not empty and has content inside.
You can do that by checking the array of vnodes
, if they are empty or not:
<script setup lang="ts">
import type { Slot, VNode } from 'vue'
import { Comment, computed, Fragment, useSlots } from 'vue'
const slots = useSlots()
function isSlotAvailable() {
return !!slots?.footer
}
// Adapted from https://github.com/vuejs/vue-next/blob/ca17162e377e0a0bf3fae9d92d0fdcb32084a9fe/packages/runtime-core/src/helpers/renderSlot.ts#L77
const isVnodeEmpty = (vnodes: Array<VNode>) => {
return vnodes.every((node: VNode) => {
if (node.type === Comment) {
return true
}
if (node.type === Text && typeof node.children === 'string' && !node.children.trim()) {
return true
}
if (
node.type === Fragment
&& isVnodeEmpty(node.children as Array<VNode>)
) {
return true
}
return false
})
}
const hasSlotContent = (slot: Slot<any> | undefined) => {
if (!slot) {
return false
}
return !isVnodeEmpty(slot())
}
</script>
<template>
<div class="container">
<h3>Hello World</h3>
<span>isSlotAvailable: {{ isSlotAvailable() }}</span>
<span>hasSlotContent: {{ hasSlotContent(slots?.footer) }}</span>
<footer v-if="slots.footer">
<h3>My Footer Heading</h3>
<slot name="footer" />
</footer>
</div>
</template>
StackBlitz
The code for this tip is interactively available in the following StackBlitz project:
If you liked this Vue tip, follow me on BlueSky to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue & Nuxt newsletter :
Vue Tip: Delay Loading Appearance of Spinner
Vue Tip: Memoize a Sub-Tree of the Template Using v-memo