Slots and scoped slots
Passing template content into a component with slots: fallback content, named slots, and scoped slots that hand child data back to the parent's markup.
Slots and scoped slots
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Props pass data; slots pass markup. A `<slot>` element in the child marks where the parent's content goes, so a component can own the frame while the parent decides what goes inside.
- Content between a `<slot>` element's tags is fallback content, shown only when the parent provides nothing for that slot.
- Named slots give a component several holes: the child writes `<slot name="header">`, the parent fills it with `<template #header>`. Anything not in a named template goes to the default slot.
- Slot content is compiled in the parent's scope. It sees the parent's state, not the child's, unless the child passes data out through slot props.
- A scoped slot passes child data out: `<slot :item="item">` in the child, `#default="{ item }"` or `v-slot="{ item }"` in the parent. The child owns the logic, the parent owns the rendering.
- `$slots` in the template and `useSlots()` in script tell the child which slots were provided, which is how optional wrappers like a footer bar are rendered conditionally.
Props aren’t enough for layout components
Build a Modal component with props and you quickly hit a wall. A title prop is fine. But the body might be a paragraph, a form, or a list of options. A body prop that takes an HTML string is unsafe and gives up components entirely. A prop for every possible variation never ends.
What the modal really wants is to own the frame (the backdrop, the close button, the focus handling) and let the caller put whatever it likes inside. Think of a picture frame: the frame maker controls the border and the glass, you bring the photo. Vue calls the hole a slot.
The default slot
A child marks the spot with a <slot> element:
<!-- AppButton.vue -->
<template>
<button class="btn">
<slot />
</button>
</template>
Whatever the parent puts between the component’s tags is rendered there:
<AppButton>
<IconSave /> Save draft
</AppButton>
The content can be text, elements, or other components. A label prop could never carry an icon component like that.
Content inside the <slot> element itself is fallback, used only when the parent provides nothing:
<button class="btn">
<slot>Submit</slot>
</button>
<AppButton /> renders “Submit”; <AppButton>Save</AppButton> renders “Save”.
Slot content lives in the parent’s scope
This is the rule that confuses people most. Slot content is compiled where it is written, in the parent’s template. It can use the parent’s state, and it cannot see the child’s:
<!-- Parent -->
<script setup>
const user = { name: 'Ada' }
</script>
<template>
<AppButton>Hello {{ user.name }}</AppButton> <!-- parent's `user`: works -->
</template>
If AppButton had its own internal state, the slot content still couldn’t read it. The same is true of JavaScript closures, covered in closures: code sees the variables where it was written, not where it is called. Scoped slots, below, are how a child passes data back out deliberately.
Named slots
Some components have more than one hole. A layout has a header, a body, and a footer. The child gives each <slot> a name:
<!-- BaseLayout.vue -->
<template>
<div class="layout">
<header><slot name="header" /></header>
<main><slot /></main>
<footer><slot name="footer" /></footer>
</div>
</template>
A <slot> without a name is the default slot. The parent targets named slots with <template v-slot:header>, almost always written with the # shorthand:
<BaseLayout>
<template #header>
<h1>Dashboard</h1>
</template>
<p>Main content goes to the default slot.</p>
<template #footer>
<small>© 2026</small>
</template>
</BaseLayout>
Top-level content that isn’t inside a named <template> goes to the default slot. You can also be explicit with <template #default>. Slot names can be dynamic: <template #[slotName]>.
Scoped slots: data back up to the parent’s markup
Now the reverse problem. A FancyList component fetches and paginates items. Different screens want to render each item differently: a card here, a table row there. The child owns the data; the parent wants to own the markup.
The child passes data to the slot as attributes. These are called slot props:
<!-- FancyList.vue -->
<script setup>
const props = defineProps({ items: Array })
</script>
<template>
<ul>
<li v-for="(item, index) in items" :key="item.id">
<slot :item="item" :index="index">
{{ item.name }} <!-- fallback rendering -->
</slot>
</li>
</ul>
</template>
The parent receives them as an object and can destructure it:
<FancyList :items="products">
<template #default="{ item }">
<strong>{{ item.name }}</strong> — {{ item.price }} €
</template>
</FancyList>
When only the default slot is used, v-slot can go directly on the component tag: <FancyList :items="products" v-slot="{ item }">. For a named scoped slot, put the props on the named template: <template #row="{ item }">. The name attribute on a <slot> is reserved and is not passed as a slot prop.
One way to picture it: a scoped slot is a callback. The child calls it once per item with arguments, and the parent’s template is the function body. That is exactly what React’s render props pattern does, and Vue’s render functions expose slots as plain functions underneath.
Taken to the extreme, a component can render nothing of its own beyond a scoped slot. This is a renderless component: it manages logic such as mouse position or selection state and hands it to the parent. In Composition API code, a composable is usually the simpler way to share pure logic. Scoped slots are still the right tool when the logic is tied to a place in the tree, such as a table that owns rows but not their markup.
Checking which slots were provided
Sometimes the child needs to know whether a slot was filled. A card shouldn’t render an empty <footer> element with padding and a border. The $slots object in the template has an entry for each slot the parent provided:
<template>
<div class="card">
<slot />
<footer v-if="$slots.footer" class="card-footer">
<slot name="footer" />
</footer>
</div>
</template>
In <script setup>, useSlots() returns the same object. With TypeScript, the defineSlots macro (3.3+) declares each slot and its props for type-checking. It is covered in TypeScript with Vue.
Slots or props?
Use a prop when the parent passes a value and the child decides how to show it: a title string, a size, a boolean. Use a slot when the parent needs to decide what gets rendered: arbitrary markup, other components, or per-item templates. Many good components use both, such as a title prop with an optional #title slot that overrides it.
What the video covers
LearnVue’s seven-minute video goes through basic slots, fallback content, named slots, and scoped slots for passing child data back to the parent, all in Vue 3 slot syntax. It predates defineSlots and doesn’t cover conditional rendering with $slots, both covered above. John Komarnicki’s short video focuses on the judgment call between a slot and a prop, and Leela Web Dev’s covers named and scoped slots together at a slower pace in the Composition API.
Check yourself
When is a slot a better choice than a prop for customising a component?
When the caller needs to supply markup or other components rather than a value: an icon plus a label inside a button, arbitrary content in a modal body, a custom cell in a table. A prop can only pass data that the child then renders in its own fixed way; a slot lets the parent render whatever it likes in a spot the child controls.
Inside `<FancyList>` the parent writes `<FancyList>{{ item.name }}</FancyList>`, expecting each list item's name. Why doesn't it work?
Slot content is compiled in the parent's scope, so `item` refers to a variable in the parent, which doesn't exist there. The child has to expose each item through a slot prop, `<slot :item="item" />`, and the parent has to receive it, `<template #default="{ item }">{{ item.name }}</template>`.
A `Card` component should only render its `<footer>` wrapper when the parent fills the `footer` slot. How?
Check whether the slot was provided: `<footer v-if="$slots.footer"><slot name="footer" /></footer>`. `$slots` holds a function for each slot the parent passed, so the wrapper element disappears entirely when no footer content is given. In script, `useSlots()` returns the same object.
What does it mean for a component to be renderless, and why do scoped slots make it possible?
A renderless component handles logic, such as fetching data, tracking mouse position, or managing selection, and renders nothing of its own beyond a scoped slot that hands its state to the parent. The parent supplies all the markup. Scoped slots are what let the child's data reach the parent's template. In Composition API code, a composable is often the simpler way to share that kind of logic.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.