📖 Scenario: You are creating a reusable list component in Vue that can display any type of items. This helps you avoid rewriting similar code for different lists in your app.
🎯 Goal: Build a generic Vue component called GenericList that accepts a list of items and a slot to render each item. Use this component to display a list of fruits.
📋 What You'll Learn
Create a Vue component named GenericList that accepts a prop items which is an array.
Add a configuration variable listTitle to hold the title of the list.
Use the v-for directive with item as the iterator variable to loop over items inside the component.
Use a default slot inside GenericList to render each item.
In the main app component, use GenericList to display a list of fruits with the title Fruits List.
💡 Why This Matters
🌍 Real World
Generic components help you write less code and reuse UI parts in many places, like lists, tables, or cards.
💼 Career
Understanding generic components is key for frontend developers to build scalable and maintainable Vue applications.
Progress0 / 4 steps
1
Create the GenericList component with items prop
Create a Vue component named GenericList with a prop called items that expects an array.
Vue
Hint
Use defineProps to declare the items prop as an array.
2
Add a listTitle variable for the list heading
Add a variable called listTitle and set it to the string 'Fruits List' inside the GenericList component.
Vue
Hint
Use ref to create a reactive variable listTitle with the value 'Fruits List'.
3
Use v-for to loop over items and render slot content
Inside the GenericList component template, use v-for with item as the iterator variable to loop over items. Render the default slot passing item as a slot prop.
Vue
Hint
Use v-for="item in items" on the <li> and pass item to the slot with :item="item".
4
Use GenericList in main app to show fruits with title
In the main app component, create a fruits array with 'Apple', 'Banana', and 'Cherry'. Use the GenericList component passing fruits as the items prop. Add a heading that displays the listTitle from GenericList using a template slot.
Vue
Hint
Import GenericList, create a fruits array, and use <GenericList :items="fruits"> with a default slot to display each fruit.
Practice
(1/5)
1. What is the main benefit of using generic components in Vue?
easy
A. They allow reusing one component for different data types.
B. They automatically style components without CSS.
C. They replace the need for Vue Router.
D. They make components load faster by default.
Solution
Step 1: Understand generic components purpose
Generic components are designed to be reusable with different data types, so you don't rewrite similar components for each type.
Step 2: Compare options to this purpose
Options B, C, and D describe unrelated features. Only They allow reusing one component for different data types. matches the core benefit of generic components.
Final Answer:
They allow reusing one component for different data types. -> Option A
Quick Check:
Generic components = reuse for many data types [OK]
Hint: Generic means reusable for many types, not styling or routing [OK]
Common Mistakes:
Confusing generic components with styling features
A. Accessing value assumes item is a ref, but it may not be.
B. Missing import of defineProps causes syntax error.
C. Generic type T must be declared before use.
D. Props must be defined as an array, not an object.
Solution
Step 1: Analyze usage of item.value
Accessing value is valid only if item is a Vue ref. The generic type T could be any type, so item might not have value.
Step 2: Check other options
Missing import of defineProps causes syntax error. is incorrect because defineProps is globally available in Vue SFCs. Generic type T must be declared before use. is incorrect because T is assumed declared in the generic context. Props must be defined as an array, not an object. is wrong because props can be defined as an object type.
Final Answer:
Accessing value assumes item is a ref, but it may not be. -> Option A
Quick Check:
Accessing .value requires ref type, generic may not be ref [OK]
Hint: Only refs have .value; generics may be plain values [OK]
Common Mistakes:
Assuming generic type is always a ref
Thinking defineProps needs import in SFC
Believing props must be array, not object
5. You want to create a generic Vue component that accepts a list of items of any type and renders each item with a slot. Which approach correctly applies generics and slots together?
hard
A. Use defineProps(['items']) and render items directly without slots.
B. Use defineProps<{ items: any[] }>() and hardcode item rendering inside component.
C. Use defineProps<{ items: T[] }>() and a default slot with v-for passing each item as slot prop.
D. Use defineProps<{ items: T }>() and render items without looping.
Solution
Step 1: Define generic props for list of items
Using defineProps<{ items: T[] }>() correctly types the prop as an array of generic type T.
Step 2: Use slots with v-for to render each item
Passing each item as a slot prop allows flexible rendering by the parent component, which is the correct pattern for generic components with slots.
Step 3: Evaluate other options
Use defineProps(['items']) and render items directly without slots. lacks generics and slots. Use defineProps<{ items: any[] }>() and hardcode item rendering inside component. uses any losing type safety and hardcodes rendering. Use defineProps<{ items: T }>() and render items without looping. uses T instead of T[] and misses looping.
Final Answer:
Use defineProps<{ items: T[] }>() and a default slot with v-for passing each item as slot prop. -> Option C
Quick Check:
Generic list + slot with v-for = Use defineProps<{ items: T[] }>() and a default slot with v-for passing each item as slot prop. [OK]
Hint: Generic list props need array type and slot with v-for [OK]
Common Mistakes:
Using single item type instead of array for list props
Not using slots for flexible item rendering
Hardcoding rendering inside generic component
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.