Vue3 Fundamentals ยท Composition & Reuse

Named slots

Named slots

One slot is often not enough. A layout wants a heading at the top and a footer at the bottom, and both come from the parent. A single default slot cannot tell which piece goes where.

Give each slot a name. The component declares an outlet per region, and the parent targets one with a v-slot directive:

<!-- the component -->
<header><slot name="header" /></header>
<footer><slot name="footer" /></footer>
<!-- the parent -->
<Layout>
  <template #header><h1>Title</h1></template>
  <template #footer><p>Footer</p></template>
</Layout>

#header is shorthand for v-slot:header. Content marked for one name lands in that name's outlet; the others stay empty. A component can mix named slots with the default one.

The Test mounts your component and passes it a header slot and a footer slot. It checks that the heading renders inside the <header> and the paragraph inside the <footer>. The starter declares only an unnamed slot, so the named content has nowhere to land and the Test fails.

Name the outlets in App.vue and the content finds them.

Challenge

The layout has a header and a footer, but only an unnamed slot, so the parent's content has nowhere to go. Export an App component from App.vue whose <header> contains <slot name="header" /> and whose <footer> contains <slot name="footer" />.

The Test passes a header slot and a footer slot, then checks the heading renders inside the <header> and the paragraph inside the <footer>. The starter has only a default slot, so the Test fails. Add the two named outlets.

App.vue
<script setup>
const title = 'Layout';
</script>

<template>
  <header>
    <h2>{{ title }}</h2>
    <slot />
  </header>
  <footer></footer>
</template>

No test run yet

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