Learn Slots in Vue

A slot is a hole in a child's template that the parent fills with markup. <slot /> is the default slot (a slot with no name): content the parent writes…

Props suit text and numbers: a dish name, a price, a stock count. But what if the Fried Rice card needs an Add button underneath, while the Chicken Noodles card needs a bold "Sold out"? Sending pieces of HTML through props is hard work.

That's what slots are for: holes in a child's template that the parent fills with markup. Props send data, slots send whole pieces of template: a title, a paragraph, a button. Picture a serving tray at Mrs. Sri's stall with compartments labelled "title", "content" and "actions". The tray's shape is always the same, but the person who serves decides what goes in each compartment. A compartment left empty shows a spare paper slip, for example "No menu yet". In Vue, the tray is the child component Card, each compartment is a slot, the person who serves is the parent, and the spare slip is the slot's spare content.

The default slot: one hole

Here is the child component Card with one hole: one <slot> with no name. A slot with no name is called the default slot.

<!-- Card.vue -->
<template>
  <div class="card">
    <slot></slot>
  </div>
</template>

The parent fills that hole by writing the content between <Card> and </Card>:

<!-- App.vue -->
<Card>
  <h3>Fried Rice</h3>
  <p>$15</p>
</Card>

The HTML that comes out:

<div class="card"><h3>Fried Rice</h3><p>$15</p></div>

Fallback content: spare content

The child can write content between <slot> and </slot>:

<!-- Card.vue -->
<div class="card">
  <slot>No menu yet</slot>
</div>
The parent writes What shows
<Card><p>Iced Tea</p></Card> Iced Tea
<Card /> No menu yet

What sits between <slot> and </slot> is called fallback content. It only shows when the parent sends nothing. So a child with <slot>Default</slot> that nobody fills shows Default.

Named slots: many holes

Now Card has three holes: title, actions and one with no name:

<!-- Card.vue -->
<template>
  <div class="card">
    <header><slot name="title">Untitled</slot></header>
    <main><slot></slot></main>
    <footer><slot name="actions"></slot></footer>
  </div>
</template>

The parent fills all three:

<!-- App.vue -->
<Card>
  <template #title><h3>Fried Rice</h3></template>
  <p>$15</p>
  <template #actions><button>Add</button></template>
</Card>

The HTML that comes out:

<div class="card">
  <header><h3>Fried Rice</h3></header>
  <main><p>$15</p></main>
  <footer><button>Add</button></footer>
</div>

📌 Scoped slots: the child lends its data

Sometimes the data is in the child, but the parent decides how it looks. The child can send data to the slot content through attributes on the <slot>. A slot that sends data to its content is called a scoped slot. In this example, the slot in each <li> sends one dish through :item="m":

<!-- MenuList.vue -->
<script setup>
const menu = [
  { name: "Fried Rice", price: 15 },
  { name: "Chicken Noodles", price: 12 },
  { name: "Iced Tea", price: 5 },
]
</script>

<template>
  <ul>
    <li v-for="m in menu" :key="m.name">
      <slot :item="m">{{ m.name }}</slot>
    </li>
  </ul>
</template>

The parent takes that item and decides how each row looks:

<!-- App.vue -->
<MenuList v-slot="{ item }">
  <b>{{ item.name }}</b> ${{ item.price }}
</MenuList>

The result is three <li>: Fried Rice $15, Chicken Noodles $12, Iced Tea $5. v-slot="{ item }" on the <MenuList> tag takes the item the child sends, so the slot content can read item.name and item.price. If the slot has a name, for example row, write <template #row="{ item }">.

.vue files and the exercise editor

In the editor, a child is written as an object and placed with <component :is>, as in the Props lesson. The slot content goes between <component> and </component>:

const Card = {
  template: `
    <div class="card">
      <header><slot name="title">Untitled</slot></header>
      <main><slot>No menu yet</slot></main>
    </div>
  `,
}

---template---
<div>
  <component :is="Card">
    <template #title><h3>Fried Rice</h3></template>
    <p>$15</p>
  </component>
  <component :is="Card" />
</div>
In a .vue file In the exercise editor
<slot> in the <template> of Card.vue <slot> in the template option of the Card object
<Card>...</Card> <component :is="Card">...</component>

Both exercises use this editor form. The checker reads the resulting HTML: in the first exercise, .card p must hold Card content. In the second, .panel-header must hold Profile and .panel-body must hold User data. The checker also reads your code: a <slot> in the child's template, and in the second exercise, a <template #header> in the parent. The <div> with the class card and the panel's two <div>s (.panel-header and .panel-body) must be written in the child's template.

⚠️ Slot content that goes to the wrong place or disappears

TL;DR: A slot is a hole in a child's template that the parent fills with markup. <slot /> is the default slot (a slot with no name): content the parent writes between the component's opening and closing tags goes there. What sits between <slot> and </slot> is fallback content (spare content), shown when the parent fills in nothing. <slot name="title"> makes a named slot, filled with <template #title> (short for v-slot:title). Content without a <template #...> goes into the default slot. Slot content reads the parent's data. A scoped slot lends the child's data to the parent: the child sends it through <slot :item="m">, and the parent takes it with v-slot="{ item }".

⚡ The default slot: one hole

Write:

<!-- Card.vue -->
<template>
  <div class="card">
    <slot></slot>
  </div>
</template>

Meaning: A slot is a hole in a child's template that the parent fills. <slot> marks where that hole is in the template of Card. This slot has no name, so it is called the default slot. Everything the parent writes between <Card> and </Card> goes there. That content is written in the parent's template, so it reads the parent's data.

⚡ Spare content

Write:

<div class="card">
  <slot>No menu yet</slot>
</div>
Guess first: the parent only writes <Card />, with nothing inside. What shows?

No menu yet. What sits between <slot> and </slot> is fallback content (spare content), shown only when the parent sends nothing.

Meaning: If the parent sends content, such as <p>Iced Tea</p>, what shows is Iced Tea, and the spare content doesn't show.

⚡ Named slots: many holes

Write:

<Card>
  <template #title><h3>Fried Rice</h3></template>
  <p>$15</p>
  <template #actions><button>Add</button></template>
</Card>
Guess first: in Card, <slot name="title"> sits inside the <header>, with the spare content Untitled. If the <h3> is written directly, without <template #title>, where does that <h3> go?

Into the default slot, because content not wrapped in a <template #...> goes there. The <header> shows its spare content instead: Untitled.

Meaning: A slot with a name is called a named slot. <slot name="title"> in the child gives the hole the name title, and the parent fills it with <template #title>, short for v-slot:title.

What you will learn