Vue3 Fundamentals ยท Composition & Reuse

Default slots

Default slots

A component often needs to wrap content it did not write. A card draws a border and a shadow, but the words inside come from wherever the card is used. If the card took those words as a prop, it would have to know every shape they could take.

A slot is a placeholder the component leaves for its parent to fill. The component marks where the content goes with a <slot> element:

<template>
  <section class="panel">
    <slot />
  </section>
</template>

Whatever the parent writes between the component's tags lands where <slot /> sits. The component owns the frame; the parent owns the content. A slot with no name is the default slot, and a component has exactly one.

The Test mounts your component and passes it a paragraph as default slot content. It then checks that the paragraph renders inside the <section>. The starter leaves the section empty, so nothing appears and the Test fails. Add the slot outlet and the content flows through.

The slot is markup, not a prop, so you never declare it in <script setup>. Use App.vue for the component.

Challenge

The panel draws its own heading but has nowhere to put the content its parent passes in. Export an App component from App.vue that renders a <section> containing a default <slot />, so the parent's markup appears inside the section.

The Test mounts your component with a paragraph as default slot content and checks the paragraph renders inside the <section>. The starter has no slot outlet, so the content is dropped and the Test fails. Add the outlet.

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

<template>
  <section class="panel">
    <h2>{{ title }}</h2>
  </section>
</template>

No test run yet

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