Chapter 55: Vue slot Element

The <slot> element

This is not just another HTML tag — it’s a special placeholder that Vue understands and replaces with content provided by the parent component.

In simple human words:

The child component says: “I have some empty spaces / holes in my design. Parent, you can decide what content goes into those holes.”

The parent then fills those holes with whatever HTML, text, or other components it wants.

This is what allows the same child component to look and behave completely differently depending on who is using it — exactly like real Lego pieces.

Why <slot> exists — the real motivation

Without slots you would have to:

  • Pass huge strings via props → ugly & unsafe (v-html)
  • Use dozens of boolean / variant props → component explodes with if conditions
  • Duplicate almost-identical components → maintenance nightmare

With slots:

  • One component can serve many different visual & functional purposes
  • Parent controls content & layout
  • Child controls structure & styling
  • Perfect separation of concerns

Types of Slots in Vue 3 (2026)

  1. Default / unnamed slot — the main content hole
  2. Named slots — multiple specific holes with names
  3. Scoped slots — child passes data back to parent (most powerful)
  4. Dynamic slot names — rare but useful

Let’s go through them one by one with real, copy-paste-ready examples.

1. Default (Unnamed) Slot — Most Common

Child component: Card.vue (simple wrapper with one main content area)

vue

Parent — different content every time

vue

→ Same <Card> wrapper — completely different inner content

2. Named Slots — Multiple Specific Places

Child can define several named insertion points.

vue

Parent usage — filling named slots

vue

Shorthand syntax (very popular):

vue

3. Scoped Slots — Child → Parent Data Passing (The Real Power)

This is where slots become extremely powerful.

The child can pass data back to the parent so the parent can decide how to render that slot using the child’s data.

Child: ProductList.vue (provides product data to parent)

vue

Parent — custom rendering using data from child

vue

→ Parent decides how each product looks — child only provides the data & loop

Quick Syntax Reference – All Slot Styles (2026)

Slot Type Child (defines slot) Parent (fills slot) – modern shorthand Parent – full verbose syntax
Default slot <slot /> <MyComp>content here</MyComp>
Named slot <slot name=”header” /> <template #header>…</template> <template v-slot:header>…</template>
Scoped slot <slot :product=”product” /> <template #product=”{ product }”>…</template> <template v-slot:product=”slotProps”>…</template>
Shorthand (no template tag) <MyComp #header>… #product=”{ product }”>…</MyComp>

Pro Tips from Real Projects (Hyderabad 2026)

  • Prefer shorthand # — much cleaner & most codebases use it
  • Always provide fallback content inside <slot> — makes component usable without custom content
  • Name slots clearly & consistently: header, footer, actions, item, empty, loading, icon, badge
  • For UI libraries → almost every reusable component should have at least one slot
  • Scoped slots + TypeScript → excellent DX (autocompletion for slot props)

Your mini practice task:

  1. Create Accordion.vue with default slot for content and named slot for title
  2. Use it 3 times with different content
  3. Add a scoped slot for icon that receives isOpen state

Any part confusing? Want to see:

  • Scoped slots with TypeScript defineSlots typing?
  • Dynamic slot names (#[dynamicSlotName])?
  • Slots vs props vs provide/inject comparison?
  • Real UI library pattern (Card, Modal, Table with multiple slots)?

Just tell me — we’ll build the next beautiful slotted component together 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *