Chapter 84: Vue v-slot Directive

V-slot directive (often written with its beautiful shorthand #)

This is the modern, official way (since Vue 2.6 / Vue 3) to work with slots — replacing the older slot=”name” and slot-scope syntax.

v-slot is what allows a parent component to inject custom content (HTML, text, other components) into specific named places inside a child component — and in the case of scoped slots, it also lets the child pass data back to the parent so the parent can decide how to render that content.

In simple human words:

The child says: “I have some holes here — header, footer, item, actions… you can put whatever you want in them.” The parent says: “Great — here’s my content for each hole. And if you give me some data about what goes in the hole, I’ll decide how to style/render it.”

Why v-slot / # is so important (real motivation)

Without slots (and v-slot), components would be very rigid:

  • You’d have to pass huge props (strings, objects, render functions) → ugly
  • Or use dozens of boolean/variant props → component explodes
  • Or duplicate almost-identical components → maintenance hell

With v-slot:

  • Same child component can look completely different depending on parent
  • Parent controls content & layout
  • Child controls structure & styling
  • Perfect separation of concerns

Types of Slots & v-slot Syntax (2026 Style)

Slot Type Child (defines) Parent (fills) – 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 :item=”item” /> <template #item=”{ item }”>…</template> <template v-slot:item=”slotProps”>…</template>
Shorthand (no template tag) <MyComp #header>… #item=”{ item }”>…</MyComp>

Real, Complete Example – Modern Card with Named & Scoped Slots

Child: FancyCard.vue (reusable card with multiple slots)

vue

Parent – different usage patterns

vue

Quick Syntax Reference – v-slot / # in 2026

Slot Type Child (defines) Parent (fills) – recommended 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 :item=”item” /> <template #item=”{ item }”>…</template> <template v-slot:item=”slotProps”>…</template>
Shorthand (no template tag) <MyComp #header>… #item=”{ item }”>…</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 even without custom slots
  • Name slots clearly & consistently: header, footer, actions, item, empty, loading, icon, badge…
  • For scoped slots → child should pass useful helpers (:index, :is-first, :formatPrice, :remove)
  • In UI libraries → almost every component should have thoughtful default slot + named slots + scoped slots
  • Scoped slots + TypeScript → excellent DX (autocompletion for slot props)

Your mini practice task:

  1. Create Modal.vue with named slots: header, body (default), footer
  2. Use it twice: once with simple text, once with form inside body
  3. Add scoped slot for footer that receives isSubmitting state from child
  4. Try shorthand syntax without <template> tags

Any part confusing? Want full examples for:

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

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 *