Default and Named Slots
Part of the Fundamentals section of Coddy's Vue journey. Lesson 39 of 55.
Slots let a parent supply markup inside a child layout. This card has a named heading outlet and a default body outlet, each with fallback content.
// Child template:
// <article><header><slot name="heading">Info</slot></header>
// <slot>Nothing yet</slot></article>
// Parent content: <template #heading><h2>Reading club</h2></template>Ordinary content between the component tags fills the default slot. A <template #heading> fills the named outlet. Slot expressions use the parent's scope, so parent refs and handlers still work inside supplied content. Fallback content appears when that slot has no supplied content.
Challenge
MediumCreate InfoPanel.vue with article.panel, a header containing the named heading slot with fallback Notice, and div.content containing the default slot with fallback No details. In #custom, supply an h2 heading Community lunch and a paragraph Bring a reusable plate. In #fallback, render the component without slot content.
Try it yourself
<!doctype html><html><head><meta charset="UTF-8"><title>Vue practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
2Reactive State
Reactive RefsUpdating Refs in ScriptEvent Handler ArgumentsReactive ObjectsRecap: Reactive State5Conditions and Lists
Conditional BranchesShow without UnmountingRendering ListsStable List IdentityRecap: Conditions8Component Communication
Emitting EventsEvent PayloadsLifting Shared StateDefault and Named SlotsRecap: Shared Panel11Project: Community Board
Board StructureAdding Community Tasks