📖 Scenario: You are building a simple webpage that shows a list of favorite fruits. Instead of writing all the HTML in one place, you want to use components to keep your code clean and easy to manage.
🎯 Goal: Create a Vue app that uses components to display a list of fruits. Each fruit should be shown using a separate component. This will help you see why components are essential for organizing your code.
📋 What You'll Learn
Create a main Vue app with a list of fruits
Create a child component called FruitItem that shows one fruit name
Use the FruitItem component inside the main app to display each fruit
Pass the fruit name as a prop to the FruitItem component
💡 Why This Matters
🌍 Real World
Websites and apps often have repeated parts like buttons, cards, or lists. Components let developers build these parts once and reuse them everywhere.
💼 Career
Understanding components is essential for frontend developer jobs using Vue or similar frameworks. It shows you can write clean, maintainable, and scalable code.
Progress0 / 4 steps
1
Set up the fruit list data
Create a Vue app with a fruits array containing these exact strings: 'Apple', 'Banana', 'Cherry'.
Vue
Hint
Use the data() function to return an object with the fruits array.
2
Create the FruitItem component
Add a Vue component called FruitItem that accepts a prop named name and displays it inside a <li> element.
Vue
Hint
Use app.component to define FruitItem with a props array and a simple template.
3
Use FruitItem component in the main app template
In the main app template, use a <ul> element and inside it, use <FruitItem> with v-for to show each fruit from fruits. Pass the fruit as the name prop.
Vue
Hint
Use v-for="fruit in fruits" and bind name with :name="fruit".
4
Mount the Vue app
Mount the Vue app to an HTML element with the id app using app.mount('#app').
Vue
Hint
Call app.mount('#app') to start the Vue app.
Practice
(1/5)
1. Why are components essential in Vue applications?
easy
A. They break a large app into smaller, manageable parts.
B. They make the app run faster by default.
C. They replace the need for HTML and CSS.
D. They automatically fix bugs in the code.
Solution
Step 1: Understand the role of components
Components divide a big app into smaller pieces that are easier to build and maintain.
Step 2: Evaluate other options
Options B, C, and D describe incorrect or exaggerated benefits not true for components.
Final Answer:
They break a large app into smaller, manageable parts. -> Option A
Quick Check:
Components simplify app structure = A [OK]
Hint: Components split big apps into small parts for easier work [OK]
Common Mistakes:
Thinking components speed up app automatically
Believing components replace HTML/CSS
Assuming components fix bugs by themselves
2. Which of the following is the correct way to define a simple Vue component?
easy
A. new Vue.component('my-comp', '
Hello
')
B. component Vue('my-comp') { template: '
Hello
' }
C. Vue.component('my-comp', { template: '
Hello
' })
D. Vue.createComponent('my-comp', { template: '
Hello
' })
Solution
Step 1: Recall Vue component syntax
The correct syntax uses Vue.component('name', { template: '...' }) to define a global component.
Step 2: Check each option
Only Vue.component('my-comp', { template: '
Hello' })
matches the correct Vue syntax; others have invalid keywords or methods.
Final Answer:
Vue.component('my-comp', { template: '<div>Hello</div>' }) -> Option C
Quick Check:
Vue.component() defines components = C [OK]
Hint: Vue.component('name', { template }) is correct syntax [OK]
Common Mistakes:
Using wrong keywords like 'component Vue' or 'new Vue.component'
Confusing method names like createComponent
Omitting the object braces around template
3. Given this Vue component code, what will be displayed?