Vue Scoped Slots
Normal slot content only sees the parent's data. But imagine a <data-list> component that loops over items: the child owns the loop, while the parent wants to decide how each item is drawn. The child must hand each item to the slot content. That is what a scoped slot does: the child passes data to the slot as slot props.
<!-- child template: pass data to the slot like props -->
<slot :item="item" :index="i"></slot>
<!-- parent: receive the slot props -->
<data-list :items="users" v-slot="{ item, index }">
{{ index + 1 }}. {{ item.name }}
</data-list>A first scoped slot
<div id="app">
<h4>Default rendering</h4>
<fancy-list :items="fruits"></fancy-list>
<h4>Custom rendering via scoped slot</h4>
<fancy-list :items="fruits" v-slot="{ item, index }">
<strong>{{ index + 1 }}.</strong> {{ item.toUpperCase() }} {{ index === 0 ? '⭐' : '' }}
</fancy-list>
</div>
<script type="module">
import { createApp } from 'vue';
const app = createApp({ data: () => ({ fruits: ['mango', 'papaya', 'lychee'] }) });
app.component('fancy-list', {
props: ['items'],
template: `
<ul>
<li v-for="(item, i) in items" :key="item">
<slot :item="item" :index="i">{{ item }}</slot>
</li>
</ul>`
});
app.mount('#app');
</script>v-slot="{ item, index }" receives an object with all the slot props; destructuring picks the ones you need. Here v-slot sits directly on the component tag, which is allowed when you only use the default slot.
Named scoped slots
Named slots can pass props too. Use #name="slotProps" on a <template>:
<style>
table { border-collapse: collapse; font-family: sans-serif; }
td, th { border: 1px solid #ddd; padding: 4px 10px; }
.neg { color: #dc2626; }
</style>
<div id="app">
<data-table :rows="accounts" :columns="['owner', 'balance']">
<template #cell-balance="{ value }">
<span :class="{ neg: value < 0 }">{{ value.toFixed(2) }} €</span>
</template>
<template #empty>No accounts yet.</template>
</data-table>
<button @click="accounts.push({ owner: 'New', balance: -5 })">Add row</button>
</div>
<script type="module">
import { createApp } from 'vue';
const app = createApp({
data: () => ({
accounts: [
{ owner: 'Tam', balance: 1250.5 },
{ owner: 'Quang', balance: -42 }
]
})
});
app.component('data-table', {
props: ['rows', 'columns'],
template: `
<table v-if="rows.length">
<thead><tr><th v-for="c in columns" :key="c">{{ c }}</th></tr></thead>
<tbody>
<tr v-for="(row, r) in rows" :key="r">
<td v-for="c in columns" :key="c">
<slot :name="'cell-' + c" :value="row[c]" :row="row">{{ row[c] }}</slot>
</td>
</tr>
</tbody>
</table>
<p v-else><slot name="empty"></slot></p>`
});
app.mount('#app');
</script>The table offers one slot per column (cell-owner, cell-balance). The parent only overrides the balance column; the owner column falls back to plain text.
Renderless components
Taken to the extreme, a component can render nothing of its own and only provide logic and data through a scoped slot. This is called a renderless component:
<div id="app">
<mouse-tracker v-slot="{ x, y }">
<div style="height:120px;background:#ecfdf5;display:grid;place-items:center">
Pointer at {{ x }}, {{ y }}
</div>
</mouse-tracker>
<toggle-state v-slot="{ on, toggle }">
<p><button @click="toggle">{{ on ? 'Hide' : 'Show' }} secret</button>
<span v-if="on">🦄 Vue loves you</span></p>
</toggle-state>
</div>
<script type="module">
import { createApp, ref } from 'vue';
const app = createApp({});
app.component('mouse-tracker', {
setup() {
const x = ref(0), y = ref(0);
const move = (e) => { x.value = e.offsetX; y.value = e.offsetY; };
return { x, y, move };
},
template: `<div @mousemove="move"><slot :x="x" :y="y"></slot></div>`
});
app.component('toggle-state', {
setup() {
const on = ref(false);
return { on, toggle: () => { on.value = !on.value; } };
},
template: `<slot :on="on" :toggle="toggle"></slot>`
});
app.mount('#app');
</script>Note: Renderless components were popular in Vue 2. In Vue 3, the same logic reuse is usually done with composables (Composition API), which don't add an extra component to the tree. Scoped slots remain the best tool when a component renders the structure and the parent customises pieces of it.
Syntax summary
| Child template | Parent template |
|---|---|
<slot :item="x"></slot> |
<comp v-slot="{ item }"> (default slot only) |
<slot :item="x"></slot> |
<template #default="{ item }"> |
<slot name="row" :item="x"></slot> |
<template #row="{ item }"> |
<slot name="row" v-bind="obj"></slot> |
every key of obj becomes a slot prop |
Tip: When a component has both a default slot and named slots, always use an explicit
<template #default="…">for the default one. Puttingv-sloton the component tag and using named templates is ambiguous and not allowed.
Test yourself
How does a child pass data to the parent's slot content?
- Through
$emit - By binding attributes on
<slot>, e.g.<slot :item="item"> - By changing a prop
- Slot content can always see child data
Which parent syntax receives the props of a named slot row?
<template #row="{ item }"><template :row="item"><row-slot item><template @row="item">
What is a renderless component?
- A component with a syntax error
- A component that only provides logic and data through a scoped slot, without its own markup
- A component hidden with
v-show - A component rendered on the server