Vue Slots
Props pass data into a component. Slots pass content — a piece of template that the parent writes and the child decides where to place. With slots you can build flexible layout components such as cards, modals, tabs and buttons that accept any markup inside them.
<!-- parent -->
<alert-box>Your changes were <strong>saved</strong>.</alert-box>
<!-- inside alert-box's template -->
<div class="alert"><slot></slot></div>The <slot> element is a placeholder ("slot outlet"); Vue replaces it with whatever the parent put between the component's tags.
A first slot
<style>
.alert { padding: 10px 14px; border-radius: 6px; margin: 6px 0; font-family: sans-serif; }
.info { background: #e0f2fe; } .success { background: #dcfce7; } .error { background: #fee2e2; }
</style>
<div id="app">
<alert-box type="success">Your order <strong>#1042</strong> was placed. 🎉</alert-box>
<alert-box type="error">
Payment failed. <button @click="retries++">Retry ({{ retries }})</button>
</alert-box>
<alert-box></alert-box>
</div>
<script type="module">
import { createApp } from 'vue';
const app = createApp({ data: () => ({ retries: 0 }) });
app.component('alert-box', {
props: { type: { type: String, default: 'info' } },
template: `
<div class="alert" :class="type">
<slot>Nothing to report.</slot>
</div>`
});
app.mount('#app');
</script>Three things to notice:
- Slot content can contain any HTML and other components.
- Slot content is compiled in the parent's scope:
{{ retries }}and@click="retries++"use the parent's data, not the child's. - Content written inside
<slot>…</slot>is the fallback, shown when the parent passes nothing (third alert).
Named slots
A component can have several slots. Give each <slot> a name, and in the parent target it with <template v-slot:name> — or the shorthand #name. Content not inside a named template goes to the default slot.
<style>
.card { border: 1px solid #ddd; border-radius: 10px; max-width: 300px; margin: 8px 0; overflow: hidden; font-family: sans-serif; }
.card header { background: #42b883; color: white; padding: 8px 12px; font-weight: bold; }
.card main { padding: 12px; }
.card footer { border-top: 1px solid #eee; padding: 8px 12px; text-align: right; }
</style>
<div id="app">
<base-card>
<template #header>🌦️ Weather</template>
<p>Rain in the afternoon, 24 °C.</p>
<template #footer><button>Details</button></template>
</base-card>
<base-card>
<template v-slot:header>📬 Inbox</template>
You have no new messages.
</base-card>
</div>
<script type="module">
import { createApp } from 'vue';
const app = createApp({});
app.component('base-card', {
template: `
<section class="card">
<header><slot name="header">Untitled</slot></header>
<main><slot></slot></main>
<footer v-if="$slots.footer"><slot name="footer"></slot></footer>
</section>`
});
app.mount('#app');
</script>$slots lets the component check which slots the parent filled. Here the <footer> is only rendered when footer content was provided.
| Syntax | Meaning |
|---|---|
<slot> |
Default slot outlet |
<slot name="header"> |
Named slot outlet |
<slot>Fallback</slot> |
Fallback content if nothing is passed |
<template v-slot:header> / <template #header> |
Fill a named slot from the parent |
<template #default> |
Explicitly fill the default slot |
$slots.footer |
Truthy when the parent provided that slot |
Building a modal dialog
Slots shine for "frame" components where the component controls the structure and behaviour, and the parent provides the content.
<style>
.backdrop { position: fixed; inset: 0; background: #0006; display: grid; place-items: center; }
.modal { background: white; border-radius: 10px; padding: 16px; min-width: 240px; font-family: sans-serif; }
.modal h3 { margin-top: 0; }
</style>
<div id="app">
<button @click="open = true">Delete account…</button>
<p>{{ status }}</p>
<modal-dialog v-if="open" @close="open = false">
<template #title>Are you sure?</template>
This cannot be undone. All {{ files }} files will be deleted.
<template #actions>
<button @click="open = false">Cancel</button>
<button @click="confirmDelete">Delete</button>
</template>
</modal-dialog>
</div>
<script type="module">
import { createApp } from 'vue';
const app = createApp({
data: () => ({ open: false, files: 37, status: '' }),
methods: {
confirmDelete() {
this.open = false;
this.status = 'Account deleted (just kidding).';
}
}
});
app.component('modal-dialog', {
emits: ['close'],
template: `
<div class="backdrop" @click.self="$emit('close')">
<div class="modal">
<h3><slot name="title"></slot></h3>
<p><slot></slot></p>
<div><slot name="actions"><button @click="$emit('close')">OK</button></slot></div>
</div>
</div>`
});
app.mount('#app');
</script>Dynamic slot names
The slot name can be dynamic, like directive arguments: <template #[slotName]>. (In in-DOM templates, keep the variable name lowercase.)
Tip: Think of props as "configure the component" and slots as "fill the component". A
<base-button>usually takes its label through the default slot, so the parent can add icons or formatting freely.
Note: Slot content can't see the child's data, because it is compiled in the parent. When the child does need to hand data to the slot content — for example, each row of a list — use a scoped slot, the topic of the next lesson.
Test yourself
What is shown when a parent passes no content to <slot>Loading…</slot>?
- Nothing
Loading…- An error
- The word
slot
What is the shorthand for <template v-slot:footer>?
<template :footer><template @footer><template #footer><template slot="footer">
In the parent's slot content, which data can {{ count }} access?
- The parent's data
- The child's data
- Both, the child wins
- Only global variables