Chapter 65: Vue $slots Object

The $slots object

This is not something you write or create yourself — Vue automatically gives it to every component instance.

$slots is basically a read-only map that tells the current component:

“Hey, the parent who used me passed me some content to insert at these named places (slots). Here is everything they gave me — go ahead and render it wherever you want.”

In modern Vue 3 + <script setup>, you almost never write $slots directly — instead you use the <slot> tag in the template, and Vue handles the rest behind the scenes.

But understanding $slots deeply will make you much better at:

  • Building reusable UI components (cards, modals, layouts, tables…)
  • Debugging slot-related bugs
  • Creating advanced scoped/named slots
  • Writing custom slot utilities or renderless components

1. Mental Model – What $slots Really Is

When a parent uses your component like this:

vue

Vue internally creates an object on the child instance that looks something like this:

JavaScript
  • Each key = slot name (default, header, footer, item…)
  • Each value = array of virtual nodes (VNodes) that the parent passed for that slot

2. How You Actually Use Slots (Modern 2026 Way)

You almost never write $slots directly — Vue handles it when you use <slot> tags.

Child component – defining slots

vue

Parent – filling slots

vue

3. Checking If a Slot Exists (Very Useful in Reusable Components)

Sometimes you want to conditionally render something only if parent provided a certain slot.

Child – check $slots

vue

4. Scoped Slots – The Real Power (Child → Parent Data Passing)

Child can pass data back to parent for that slot.

Child – providing data

vue

Parent – using scoped slot data

vue

5. Quick Summary Table – $slots in 2026

Question Answer / Behavior
Do I import $slots? No — Vue injects it automatically in every component
Is $slots reactive? Yes — changes when parent re-renders with different slot content
What does $slots contain? Object where keys = slot names, values = array of VNodes
Default slot key? ‘default’
How to check if slot exists? if ($slots.header) or if ($slots[‘header’])
Scoped slot? Parent receives data via destructuring: #product=”{ product }”
Fallback content? Put it inside <slot> — shown only if parent doesn’t provide slot
Named slot shorthand? <template #header> (modern & most common)

Pro Tips from Real Projects (Hyderabad 2026)

  • Always provide fallback content inside <slot> — makes component usable without custom slots
  • Use named slots for complex layouts (header, footer, actions, item, empty, loading…)
  • Prefer scoped slots when child has data parent might want to customize rendering with
  • Use $slots checks (v-if=”$slots.header”) to conditionally render wrappers or fallbacks
  • In UI libraries → almost every component should have thoughtful default slots + named slots + scoped slots
  • Scoped slots + TypeScript → excellent DX (autocompletion for slot props)

Your mini practice task:

  1. Create Card.vue with named slots: header, body (default), footer
  2. Use it twice — once with only default slot, once with all named slots
  3. Add v-if=”$slots.footer” → show different footer styling when footer is provided
  4. Add scoped slot #actions that receives isLoading prop from child

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 *