Vue3 Fundamentals ยท Composition & Reuse

Scoped slots

Scoped slots

Sometimes the component owns the data and the parent owns the markup. A list knows its items; the parent decides how each item looks. A plain slot cannot carry that data out, because the parent writes the content before the list runs.

A slot can pass values back to the parent. Bind them on the <slot> element:

<script setup>
const item = { name: 'Vue' };
</script>

<template>
  <ul>
    <slot :item="item" />
  </ul>
</template>

The parent receives those bindings through the slot's scope and uses them in its own template:

<List>
  <template #default="{ item }">
    <li>{{ item.name }}</li>
  </template>
</List>

The component decides what to pass; the parent decides how to render it. That is a scoped slot.

The Test mounts your component and gives it a scoped slot that reads item and renders its name. It checks the rendered text. The starter renders a plain slot with no bindings, so item is undefined and the Test fails. Pass the item out from App.vue.

Challenge

The list owns an item object but the parent cannot reach it through a plain slot. Export an App component from App.vue that renders <slot :item="item" /> inside a <ul>, exposing the item to the parent's slot content.

The Test mounts your component with a scoped slot that reads item and renders its name, then checks the text. The starter passes no bindings, so item is undefined and the Test fails. Bind the item on the slot.

App.vue
<script setup>
const item = { name: 'Vue' };
</script>

<template>
  <ul>
    <slot />
  </ul>
</template>

No test run yet

The Test is hidden. Run it to check the Challenge โ€” you only see the result, never the test source.