📖 Scenario: You are building a simple card component in Vue that can display different content in its header and footer areas. You want to use named slots so users can put custom content in these areas. Also, you want to pass some data from the card to the slot content using scoped slots.
🎯 Goal: Create a Vue component called BaseCard that uses named slots for header and footer. Pass a title string from the component to the header slot using a scoped slot. Then use BaseCard in an app and provide custom content for the named slots.
📋 What You'll Learn
Create a Vue component named BaseCard with a title prop set to 'Welcome'.
Inside BaseCard, define named slots called header and footer.
Pass the title prop to the header slot as a scoped slot property.
Use BaseCard in the main app and provide custom content for the header and footer slots.
In the header slot content, display the passed title property.
💡 Why This Matters
🌍 Real World
Named and scoped slots let you build flexible UI components that can be customized by users without changing the component code.
💼 Career
Understanding slots is essential for Vue developers to create reusable components and collaborate on large projects.
Progress0 / 4 steps
1
Create the BaseCard component with a title prop
Create a Vue component called BaseCard with a props option that defines a title prop with the default value 'Welcome'.
Vue
Hint
Use props: { title: { type: String, default: 'Welcome' } } inside the component.
2
Add named slots for header and footer in BaseCard template
Add a template section to BaseCard with a <div> container. Inside it, add two named slots: one named header and one named footer. Use <slot name="header"></slot> and <slot name="footer"></slot>.
Vue
Hint
Use <slot name="header"></slot> and <slot name="footer"></slot> inside the template.
3
Pass the title prop to the header slot as a scoped slot
Modify the header slot in BaseCard to pass the title prop as a scoped slot property named title. Use <slot name="header" :title="title"></slot>.
Vue
Hint
Add :title="title" to the header slot tag to pass the prop as a scoped slot.
4
Use BaseCard with named slots and display scoped slot title
In the main app component, import BaseCard and use it. Provide content for the header slot using <template #header="{ title }"> that displays the title inside an <h1>. Provide content for the footer slot with a <p> that says 'Footer content here'.
Vue
Hint
Use <template #header="{ title }"> to access the scoped slot property and display it inside an <h1>.
Practice
(1/5)
1. What is the main purpose of named slots in Vue components?
easy
A. To handle component lifecycle hooks
B. To share data from parent to child components
C. To create reactive state inside a component
D. To place different content in specific parts of a component
Solution
Step 1: Understand named slots concept
Named slots allow you to define multiple placeholders inside a component where different content can be inserted.
Step 2: Identify the purpose of named slots
They help organize content by placing it in specific parts of the component template.
Final Answer:
To place different content in specific parts of a component -> Option D
Quick Check:
Named slots = specific content placement [OK]
Hint: Named slots = different content in specific places [OK]
Common Mistakes:
Confusing named slots with props
Thinking named slots share data
Mixing named slots with lifecycle hooks
2. Which of the following is the correct syntax to define a named slot called header inside a Vue component?
easy
A.
B.
C.
D.
Solution
Step 1: Recall named slot syntax in Vue
Named slots are defined using the element with a name attribute.
Step 2: Match correct syntax
The correct syntax is . Other options are invalid or deprecated.
Assuming MyComponent defines a named slot footer using <slot name="footer">.
medium
A. Missing v-slot directive on template
B. Using deprecated slot attribute instead of #footer shorthand
C. Named slot must be inside MyComponent template, not usage
D. No error, this is correct syntax
Solution
Step 1: Check slot syntax in Vue 3
The slot attribute on template is deprecated in Vue 3.
Step 2: Identify correct syntax for named slots
Use v-slot:footer or shorthand #footer instead of slot="footer".
Final Answer:
Using deprecated slot attribute instead of #footer shorthand -> Option B
Quick Check:
Use v-slot or # for named slots [OK]
Hint: Use v-slot or #, not slot attribute [OK]
Common Mistakes:
Using old slot attribute syntax
Confusing slot definition with slot usage
Thinking no syntax change in Vue 3
5. You want to create a reusable Card component with named slots for header, body, and footer. The body slot should receive a scoped prop info from the component. Which is the correct way to define the Card template?
hard
A.
B.
C.
D.
Solution
Step 1: Identify named slots and scoped props
The body slot must receive the scoped prop info from the component's data.
Step 2: Check slot definitions
Only the body slot has :info='cardInfo' to pass the scoped prop. The others are simple named slots without props.
Final Answer:
Correct template with named slots and scoped prop on body slot -> Option A
Quick Check:
Scoped prop on body slot only [OK]
Hint: Pass scoped props only on slots needing data [OK]
Common Mistakes:
Passing scoped props on wrong slots
Missing scoped prop on body slot
Using default slot instead of named slots
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.