DEV Community


Posted on

Check if named slot exists in your page with Vue.js

In the Composition API we have the runtime function useSlots() that can be used to check if our slot exist or not, to do that we need to import it from Vue and check the existence of slot directly in template.

            <slot name="myNamedSlot" />

<script setup>
import { useSlots } from 'vue'

// this const will be avaiable on template
const slots = useSlots()

Enter fullscreen mode Exit fullscreen mode

Attention: slots elements are dynamic and not reactive, so we need to check them directly in template.

That's it!


Top comments (1)