π§© Vue Named Slots (v-slot) β Organize and Customize Component Content (2025 Guide)
π§² Introduction β What Are Named Slots in Vue?
In Vue.js, named slots give developers the ability to insert content into specific sections of a component. This is an enhancement over default slots, providing more control and flexibility, especially for layout and component composition.
π― In this guide, youβll learn:
- The syntax of
v-slotand how named slots work - How to pass content into multiple regions of a component
- How to use shorthand and scoped slots with
v-slot - Best practices and common use cases
π What Is a Named Slot?
A named slot allows the parent to inject content into a specific slot region in the child component using the name attribute on the <slot> element.
β Syntax in Child:
<slot name="header"></slot>
<slot name="footer"></slot>
β Usage in Parent:
<template #header>
<h1>Welcome!</h1>
</template>
<template #footer>
<p>Footer content here</p>
</template>
π§ Using v-slot Directive
In Vue 3, named slots are used via the v-slot directive on <template> tags.
β Full Example:
<MyLayout>
<template v-slot:header>
<h1>Page Title</h1>
</template>
<p>Main content here</p>
<template v-slot:footer>
<p>Β© 2025 Company</p>
</template>
</MyLayout>
π§± Creating Named Slots in a Component
β LayoutComponent.vue
<template>
<div class="layout">
<header><slot name="header" /></header>
<main><slot /></main> <!-- default slot -->
<footer><slot name="footer" /></footer>
</div>
</template>
This allows the parent component to inject content into three distinct areas: header, main, and footer.
π― Default Slot + Named Slots
You can use both types in the same component:
<slot name="title" />
<slot /> <!-- default content -->
<slot name="actions" />
π Shorthand Syntax for v-slot
Vue allows a shorthand for v-slot using #:
<template #header><h1>Title</h1></template>
<template #default><p>Body text</p></template>
<template #footer><p>Footer</p></template>
π§ # is equivalent to writing v-slot:name.
𧬠Real-World Example β Card with Named Slots
β CardComponent.vue
<template>
<div class="card">
<header><slot name="title"></slot></header>
<section><slot></slot></section>
<footer><slot name="footer"></slot></footer>
</div>
</template>
β Parent Usage:
<CardComponent>
<template #title>
<h2>Card Title</h2>
</template>
<p>This is the card body.</p>
<template #footer>
<button>Confirm</button>
</template>
</CardComponent>
π§ This pattern gives component users the power to define their own content in each area.
π Dynamic Content with Named Slots
Named slots are often used in:
- Layout systems
- Tab components
- Cards and panels
- Dialogs and modals
π« Common Mistakes to Avoid
β Mismatched slot names
β
Use matching names in child <slot name="x" /> and parent v-slot:x
β Misplaced default slot content
β
Use default content outside of v-slot templates
β Mixing named slot with static content
β
Keep dynamic slots inside <template v-slot:name>
π Summary β Recap & Next Steps
Vue named slots let you build powerful and flexible component interfaces by allowing parents to define what appears where. Theyβre essential for building customizable and scalable UIs.
π Key Takeaways:
- Use
v-slot:nameto target a named<slot>in child - Default slot can coexist with named slots
- Use
#nameas shorthand forv-slot:name - Enable dynamic layout creation in reusable components
βοΈ Real-World Relevance:
Named slots are perfect for dashboards, modals, card layouts, form wrappers, and admin panels.
β FAQ Section
β What is a named slot in Vue?
β
A named slot is a <slot> in a component template with a name attribute. It allows targeted content injection from the parent.
β How do I use the v-slot directive?
β
Use it on a <template> inside the parent:
<template v-slot:header>Header Content</template>
β Can I use default and named slots together?
β
Yes. The unnamed <slot> acts as the default, and named slots are handled separately.
β What is the shorthand for v-slot:name?
β
Use #name, like this:
<template #footer><p>Footer text</p></template>
β Can I use scoped data with named slots?
β
Yes, named slots can be scoped just like default slots using v-slot:name="slotProps".
Share Now :
