Vue Slots & Advanced Components
Estimated reading: 4 minutes 28 views

πŸ” Vue Scoped Slots – Pass Data from Child to Parent Templates (2025 Guide)


🧲 Introduction – What Are Scoped Slots in Vue?

Scoped slots in Vue.js allow child components to expose data to the parent through slots. Unlike regular slots, which only pass HTML content from parent to child, scoped slots pass both content and logic, enabling truly dynamic UIs.

🎯 In this guide, you’ll learn:

  • What scoped slots are and how they differ from regular slots
  • How to use the v-slot directive with slot props
  • Real-world examples and use cases
  • Best practices and common pitfalls

πŸ“˜ What Is a Scoped Slot?

Scoped slots allow a child component to pass data (slot props) back to the parent component, which then uses that data to dynamically render content.

🧠 This enables customized rendering logic in the parent, even though the data is owned by the child.


🧱 Basic Scoped Slot Syntax

βœ… Child Component (ListRenderer.vue)

<template>
  <ul>
    <li v-for="item in items" :key="item.id">
      <slot :item="item" />
    </li>
  </ul>
</template>

<script>
export default {
  props: ['items']
}
</script>

βœ… Parent Usage

<ListRenderer :items="products">
  <template v-slot:default="{ item }">
    <strong>{{ item.name }}</strong> – ${{ item.price }}
  </template>
</ListRenderer>

🧠 The parent receives each item as a scoped prop and renders it as needed.


πŸ”§ Why Use Scoped Slots?

βœ… Child defines structure and data
βœ… Parent controls presentation and rendering logic
βœ… Great for reusable components like:

  • List rendering
  • Table layouts
  • Dynamic dropdowns

πŸ§ͺ Full Example – User Card Renderer

βœ… UserList.vue (Child)

<template>
  <div>
    <slot v-for="user in users" :user="user" :key="user.id" />
  </div>
</template>

<script>
export default {
  props: ['users']
}
</script>

βœ… Parent Usage

<UserList :users="userData">
  <template v-slot:default="{ user }">
    <div class="card">
      <h3>{{ user.name }}</h3>
      <p>Email: {{ user.email }}</p>
    </div>
  </template>
</UserList>

🧠 Parent uses the data (user) to render the layout it wants, keeping the logic flexible.


🧬 Shorthand & Naming Slots

You can name the slot and use #name shorthand:

<template #user="{ user }">
  <p>{{ user.name }}</p>
</template>

In child:

<slot name="user" :user="userData" />

🎯 Use Case – Table Header Renderer

Scoped slots are ideal for custom table headers:

βœ… <DataTable> (Child)

<template>
  <table>
    <thead>
      <tr>
        <slot name="header" />
      </tr>
    </thead>
    <tbody>
      <tr v-for="row in data" :key="row.id">
        <slot name="row" :row="row" />
      </tr>
    </tbody>
  </table>
</template>

βœ… Parent:

<DataTable :data="items">
  <template #header>
    <th>Name</th><th>Price</th>
  </template>

  <template #row="{ row }">
    <td>{{ row.name }}</td><td>{{ row.price }}</td>
  </template>
</DataTable>

⚠️ Common Mistakes to Avoid

❌ Mistakeβœ… Solution
Forgetting v-slot syntaxAlways bind with v-slot:name="{ data }"
Not using :key in loopsAdd :key="item.id" in v-for elements
Mixing up props vs slotsProps pass data from parent β†’ child; scoped slots pass data from child β†’ parent

πŸ“Œ Summary – Recap & Next Steps

Scoped slots make components more powerful by giving data control to the parent while keeping logic inside the child. This creates flexible, reusable layoutsβ€”ideal for advanced UIs.

πŸ” Key Takeaways:

  • Scoped slots allow child components to expose data to parent templates
  • Use v-slot:name="{ slotProps }" in the parent
  • Ideal for rendering lists, tables, and custom dropdowns
  • Combine with default and named slots for dynamic interfaces

βš™οΈ Real-World Relevance:
Scoped slots power dashboards, data tables, cards, dropdowns, modals, and reusable content renderers in large Vue apps.


❓ FAQ Section

❓ What is a scoped slot in Vue?

βœ… A slot that passes data (slot props) from the child component to the parent for dynamic rendering.


❓ How do I access slot props?

βœ… Use the v-slot directive in the parent:

<template v-slot:default="{ item }">{{ item.name }}</template>

❓ Can scoped slots and named slots be used together?

βœ… Yes. Each named slot can also be scoped by passing props:

<slot name="header" :data="someValue" />

❓ What’s the difference between props and scoped slots?

βœ… Props send data down from parent to child. Scoped slots send data up from child to parent.


❓ Do I need scoped slots in every component?

βœ… No. Use them only when the parent needs control over rendering using child data.


Share Now :

Leave a Reply

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

Share

Vue Scoped Slots

Or Copy Link

CONTENTS
Scroll to Top