Thuta Learning
Vue.js
IntermediateWeb DevelopmentBeginner

Slots and Flexible Components

Vue.jsLesson 1320 min

What you'll walk away with

  • Provide slot content
  • Use named slots
  • Receive data through scoped slots

Explain it simply

Slots and Flexible Components, in plain language

Just as props pass data, slots pass template content. Slots are a great fit for wrapper components like cards, modals, and layouts. Named slots let you split things into header/body/footer, and scoped slots let a child hand its data back to the parent's template.

Problem → Why → Solution

The problem — why this concept matters

Mixing up what slots are for versus what props are for

A common mistake

Ending up with an empty UI because no fallback content was provided

Core concept — understand how it works

Build reusable layout components with default, named, and scoped slots.

Mental model
  1. props or event
  2. component and template
  3. rendered Vue UI

Provide slot content

Core syntax or pattern

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

<!-- usage -->
<BaseCard>
  <template #title>Vue Course</template>
  <p>20 practical lessons</p>
  <template #actions><button>Start</button></template>
</BaseCard>

Build reusable layout components with default, named, and scoped slots.

Read the code step by step

  1. props or event<!-- BaseCard.vue -->

    Just as props pass data, slots pass template content. Slots are a great fit for wrapper components like cards, modals, and layouts. Named slots let you split things into header/body/footer, and scoped slots let a child hand its data back to the parent's template.

  2. component and template</template>

    Build reusable layout components with default, named, and scoped slots.

  3. rendered Vue UI</BaseCard>

    Apply Slots and Flexible Components in a small example and test normal, edge, and failure cases.

Execution flow
  1. props or event
  2. component and template
  3. rendered Vue UI

Success

Provide slot content

Failure

Mixing up what slots are for versus what props are for

Let's break it down simply

Just as props pass data, slots pass template content. Slots are a great fit for wrapper components like cards, modals, and layouts. Named slots let you split things into header/body/footer, and scoped slots let a child hand its data back to the parent's template.

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

<!-- usage -->
<BaseCard>
  <template #title>Vue Course</template>
  <p>20 practical lessons</p>
  <template #actions><button>Start</button></template>
</BaseCard>
You should see
Vue Course
20 practical lessons
[Start]

Try it yourself

Write a BaseModal component with named slots for header, content, and footer.

Slots — Vue.js

Real-world usage

How to apply it in a real project

Apply Slots and Flexible Components in a small example and test normal, edge, and failure cases.

Mini challenge

Build it yourself

Write a BaseModal component with named slots for header, content, and footer.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
vue
<!-- BaseCard.vue -->
<template>
  <article class="card">
    <header><slot name="title">Untitled</slot></header>
    <div><slot /></div>
    <footer><slot name="actions" /></footer>
  </article>
</template>

<!-- usage -->
<BaseCard>
  <template #title>Vue Course</template>
  <p>20 practical lessons</p>
  <template #actions><button>Start</button></template>
</BaseCard>

Expected: Provide slot content

Show hint

Apply Slots and Flexible Components in a small example and test normal, edge, and failure cases.

Quick quiz 1 / 3

What best describes the core idea of Slots and Flexible Components?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • props or event → component and template → rendered Vue UI
  • Build reusable layout components with default, named, and scoped slots.
  • Mixing up what slots are for versus what props are for
  • Apply Slots and Flexible Components in a small example and test normal, edge, and failure cases.

Slots — Vue.js

Easy traps

  • Mixing up what slots are for versus what props are for
  • Ending up with an empty UI because no fallback content was provided

Exercise

Write a BaseModal component with named slots for header, content, and footer.

You'll know it worked when: Vue Course 20 practical lessons [Start]

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Slots and Flexible Components | Thuta Learning