Vue.js course ยท Module 8: Slots & Dynamic Components

Named Slots

4 min read ยท
In this lesson4

The NOVA LAB storage has many sections - a header with a label, the main cargo chamber and a control panel at the bottom. Try to build such a panel with a single default slot and you quickly hit a wall: everything the parent passes lands in one place, so there is no way to send the "Seal Bay" button to the footer and the label to the header. You need several docking ports in one module, each with its own address. Vue lets you define multiple slots in a single component - these are named slots.

Defining Named Slots

In the child component you give slots names with the name attribute. The StoragePanel below has three ports: header in the header, an unnamed slot in the main area and footer in the footer, and each of them got its own fallback content:

<!-- StoragePanel.vue -->
<template>
  <div class="storage-panel">
    <header class="panel-header">
      <slot name="header">
        <h2>Default Header</h2>
      </slot>
    </header>

    <main class="panel-body">
      <slot>
        <!-- Slot without name = "default" slot -->
        <p>No content loaded</p>
      </slot>
    </main>

    <footer class="panel-footer">
      <slot name="footer">
        <p>Standard footer</p>
      </slot>
    </footer>
  </div>
</template>

The slot without a name attribute did not disappear - it is still the default port, and Vue gives it the implicit name default. The fallback from the previous lesson works in each port separately: an empty header shows "Default Header" even if the footer is filled.

Filling Named Slots

The parent directs content to a specific port with the v-slot directive, using the slot name as its argument. You put the directive on a <template> tag, which is only an invisible wrapper and creates no element in the DOM:

<template>
  <StoragePanel>
    <template v-slot:header>
      <h2>Cargo Bay Alpha</h2>
    </template>

    <!-- Content without template goes to the default slot -->
    <p>Main cargo content here</p>

    <template v-slot:footer>
      <button>Seal Bay</button>
    </template>
  </StoragePanel>
</template>

The paragraph without <template> goes to the default slot. The order of the blocks in the parent doesn't matter: even if you wrote the footer at the very top, the button would still land at the bottom, because the child's template decides where the ports are. If you see a slot="header" attribute on a plain element in older code, that is Vue 2 syntax - in Vue 3 it no longer routes content to a port, and such an element ends up in the default slot.

Shorthand Syntax:

Just as : is short for v-bind and @ is short for v-on, the # sign is short for v-slot:. Instead of v-slot:header you can write #header, and mark the default slot explicitly as #default:

<template>
  <StoragePanel>
    <template #header>
      <h2>Cargo Bay Alpha</h2>
    </template>

    <template #default>
      <p>Main content</p>
    </template>

    <template #footer>
      <button>Seal Bay</button>
    </template>
  </StoragePanel>
</template>

The result on screen is identical to the full syntax - only the notation changed. It's worth recognizing the full v-slot: form, because you will meet it in older examples and with dynamic slot names, which you will learn in the lesson on advanced patterns.

Conditional Slots

Sometimes a section only makes sense when the parent has filled it - an empty footer with a border looks like a broken panel. The useSlots() function returns a slots object in which every passed slot is available under its name, while a slot that wasn't passed is undefined. So all you need is to check it in v-if:

<!-- StoragePanel.vue -->
<script setup>
import { useSlots } from 'vue'
const slots = useSlots()
</script>

<template>
  <div class="panel">
    <header v-if="slots.header" class="header">
      <slot name="header" />
    </header>

    <main>
      <slot />
    </main>

    <footer v-if="slots.footer" class="footer">
      <slot name="footer" />
    </footer>
  </div>
</template>

When the parent doesn't pass a footer, the <footer> element never reaches the DOM, so no empty frame is left behind. The slots themselves work exactly as before - v-if only decides about the wrapper. In the template you can also use $slots.header without an import; it is the same object, exposed to the template automatically.

My advice: use the # shorthand, because it is shorter and it is what most examples in the Vue documentation use. Also name ports by their role, not their look - actions rather than bottomRight - because the panel layout may change, but the role stays. In the next lesson we will reverse the direction: the child will pass data to the slot content. In the editor below, StoragePanel appears twice: once with all three slots and once without the footer, so you can see how v-if hides the empty section.

Remember: named slots are several docking ports in one module - the child names the ports, and the parent addresses the cargo to the right one.

Code for this lesson: App.vue
<script setup>
import StoragePanel from './StoragePanel.vue'
</script>

<template>
  <div class="nova-lab">
    <h1>Named Slots Demo</h1>

    <!-- All slots filled -->
    <StoragePanel>
      <template #header>
        <h2>Cargo Bay Alpha</h2>
        <span class="badge">ACTIVE</span>
      </template>

      <template #default>
        <p>Oxygen Tank OX-2087 - loaded</p>
        <p>Water Filter WF-01 - pending</p>
      </template>

      <template #footer>
        <button class="btn">Seal Bay</button>
        <button class="btn secondary">Inspect</button>
      </template>
    </StoragePanel>

    <!-- Only header and default -->
    <StoragePanel>
      <template #header>
        <h2>Cargo Bay Beta</h2>
      </template>

      <p>Empty bay - ready for loading</p>
    </StoragePanel>
  </div>
</template>

<style scoped>
.nova-lab {
  background: #0a0e27;
  color: #00ff88;
  min-height: 100vh;
  padding: 20px;
  font-family: monospace;
}
h1 { color: #00ff88; margin-bottom: 20px; }
.badge {
  background: #00ff88;
  color: #0a0e27;
  padding: 2px 8px;
  font-size: 11px;
  font-weight: bold;
}
.btn {
  background: #00b4d8;
  color: #0a0e27;
  border: none;
  padding: 8px 16px;
  cursor: pointer;
  margin-right: 8px;
  font-family: monospace;
  font-weight: bold;
}
.btn.secondary {
  background: transparent;
  border: 1px solid #00b4d8;
  color: #00b4d8;
}
</style>

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. How do you define a named slot in a child component?

  2. 2. How do you pass content to a named slot called 'header' in the parent?

These are 2 of 3 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Code editor

    Create a StationPanel component with three named slots: header, default, and footer. Fill them with content in App.vue.

  • Horizontal ordering

    Arrange the shorthand syntax for passing content to a named slot called 'actions':

  • Code editor

    Create a MarsLayout component with named slots: sidebar, main, and footer. Use the # shorthand syntax in the parent.

  1. QuestionNamed slot syntax in child
  2. QuestionPassing content to a named slot
  3. Coding taskPractice: Named slots
  4. Put in orderShorthand v-slot syntax
  5. Question# shorthand for v-slot
  6. Coding taskPractice: Layout with named slots

Useful articles