📖 Scenario: You are building a reusable Card component for a blog website. The card should have a title area and a content area. You want to allow users of the card to insert their own title and content inside the card using Vue slots.
🎯 Goal: Create a Vue component called Card that uses slots to let users insert custom title and content. Then use this component in a parent component, passing different titles and content inside the slots.
📋 What You'll Learn
Create a Card component with two slots: one named title and one default slot for content.
Use the Card component in a parent component and pass custom HTML inside the title slot and default slot.
Ensure the slots render the passed content inside the card layout.
💡 Why This Matters
🌍 Real World
Slots let you build flexible, reusable UI components that can accept different content from their users. This is common in design systems and component libraries.
💼 Career
Understanding slots is essential for Vue developers to create modular and maintainable components that adapt to various use cases.
Progress0 / 4 steps
1
Create the Card component with slots
Create a Vue component called Card with a template that has a <div> with class card. Inside it, add a <header> with a named slot called title and a <section> with a default slot for content. Use <slot name="title">Default Title</slot> and <slot>Default content</slot> exactly.
Vue
Hint
Use <slot name="title">Default Title</slot> inside the header and <slot>Default content</slot> inside the section.
2
Import and register the Card component
In a parent Vue component, import the Card component from './Card.vue' and register it locally using components. Use the <script setup> syntax and add import Card from './Card.vue' at the top.
Vue
Hint
Use import Card from './Card.vue' at the top of the script setup block.
3
Use the Card component with slots in the parent template
In the parent component template, add the <Card> component. Inside it, add a <template v-slot:title> with an <h2> containing the text My Blog Post. Also add a paragraph <p> inside the default slot with the text This is the blog post content. Use exactly <template v-slot:title> syntax.
Vue
Hint
Use <template v-slot:title> to pass the title slot content and place the paragraph directly inside <Card> for the default slot.
4
Add basic styles to the Card component
In the Card.vue component, add a <style scoped> block. Inside it, add CSS to give the .card class a border of 1px solid #ccc, padding of 1rem, and border-radius of 0.5rem. This will visually separate the card content.
Vue
Hint
Use a scoped style block and add CSS rules for the .card class as described.
Practice
(1/5)
1. What is the main purpose of slots in Vue components?
easy
A. To define component data properties
B. To handle component lifecycle events
C. To insert custom content inside a component from its parent
D. To style components with CSS
Solution
Step 1: Understand slot usage
Slots allow a parent component to pass custom content into a child component's template.
Step 2: Differentiate from other features
Slots are not for data, lifecycle, or styling but for content distribution.
Final Answer:
To insert custom content inside a component from its parent -> Option C
Quick Check:
Slots = custom content insertion [OK]
Hint: Slots let parents insert content inside child components [OK]
Common Mistakes:
Confusing slots with data properties
Thinking slots handle lifecycle events
Assuming slots are for styling
2. Which of the following is the correct syntax to define a named slot called header in a Vue component?
easy
A. <template slot="header"></template>
B. <slot name="header"></slot>
C. <slot>header</slot>
D. <named-slot name="header"></named-slot>
Solution
Step 1: Recall named slot syntax
Named slots use <slot name="slotName"></slot> inside the child component.
Step 2: Check options for correct syntax
<slot name="header"></slot> matches the correct Vue syntax for named slots.
What will be rendered inside the <header> and <main> tags?
medium
A. Header Content inside <header>, Default Content inside <main>
B. Default Header inside <header>, Default Content inside <main>
C. Header Content inside <header>, Default Content inside <main> replaced by nothing
D. Default Header inside <header>, Default Content inside <main> replaced by nothing
Solution
Step 1: Identify slot content passed from parent
The parent provides content for the named slot "header" and default slot content "Default Content".
Step 2: Match slots in child template
The named slot "header" receives "Header Content" overriding default header text. The default slot receives "Default Content" from parent, so default fallback is not used.
A. The parent uses #foot but child expects slot named "footer"
B. The child has two slots but parent provides content for none
C. The default slot is missing a name attribute
D. Slots cannot have default content inside them
Solution
Step 1: Compare slot names between parent and child
The child expects a named slot "footer" but the parent provides content for "foot" slot, which does not match.
Step 2: Understand slot matching behavior
Because of the name mismatch, the child's "footer" slot will show its default content, and the parent's "foot" slot content is ignored.
Final Answer:
The parent uses #foot but child expects slot named "footer" -> Option A
Quick Check:
Slot names must match exactly [OK]
Hint: Slot names must match exactly between parent and child [OK]
Common Mistakes:
Assuming default slot must have a name
Thinking slots cannot have default content
Ignoring slot name mismatches
5. You want to create a reusable Card component with three slots: header, body, and footer. The body slot should have default text "No content provided" if empty. Which child component template correctly implements this?