Slots Introduction
A slot is a placeholder inside a child component. The parent fills the slot with its own markup.
A slot in the child
The slot tag marks the spot for parent content.
const Card = defineComponent({
template: '<div class="card"><slot></slot></div>'
})Filling the slot
The words between the tags fill the slot.
<Card>Welcome back.</Card>Different content
The same child wraps new words.
<Card>Goodbye for now.</Card>Example
<script setup>
import { defineComponent } from 'vue'
const Card = defineComponent({
template: '<div class="card"><slot></slot></div>'
})
</script>
<template>
<div>
<Card>Welcome back.</Card>
</div>
</template>
Key points
- The child places a slot tag where the content goes.
- The parent writes content between the child tags.
- The content renders in place of the slot.
- A slot lets one child wrap many kinds of content.
- The child controls the frame, and the parent supplies the words.
In this example, Card is a child with a slot inside a div. The parent places the words Welcome back inside Card. The words render in place of the slot. The card frame comes from the child.
Note
A slot with no content from the parent renders nothing.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does a slot do?