📖 Scenario: You are building a simple Vue 3 app that shows a list of products with their prices. You want to calculate the total price dynamically whenever the prices change.
🎯 Goal: Create a Vue 3 component using the Composition API that holds a list of products and their prices, then use a computed property to calculate the total price. Display the total price in the template.
📋 What You'll Learn
Use the Vue 3 Composition API with setup() function
Create a reactive list of products with names and prices
Create a computed property to calculate the total price
Display the total price in the template inside a <strong> tag
💡 Why This Matters
🌍 Real World
Computed properties are used in Vue apps to automatically update displayed values when underlying data changes, such as totals, filtered lists, or derived states.
💼 Career
Understanding computed properties and the Composition API is essential for modern Vue development, a skill required for frontend developer roles working with Vue.js.
Progress0 / 4 steps
1
Set up the products list
Inside the setup() function, create a reactive variable called products using ref that holds an array of objects with these exact entries: { name: 'Apple', price: 1.2 }, { name: 'Banana', price: 0.8 }, and { name: 'Cherry', price: 2.5 }.
Vue
Hint
Use ref from Vue to create a reactive array inside setup().
2
Create a computed property for total price
Import computed from 'vue' and create a computed property called totalPrice that calculates the sum of all price values in the products array.
Vue
Hint
Use computed to create totalPrice that sums prices using reduce.
3
Display the total price in the template
Inside the <div> in the template, add a <strong> tag that shows the text Total Price: followed by the value of totalPrice. Use Vue's mustache syntax {{ }} to bind the computed value.
Vue
Hint
Use {{ totalPrice }} inside a <strong> tag to show the computed value.
4
Add product list display
Below the <strong> tag, add an unordered list <ul>. Use v-for with product and index to loop over products. Inside the loop, display each product's name and price in a list item <li>. Add a unique :key binding using index.
Vue
Hint
Use v-for="(product, index) in products" on <li> with :key="index" to list each product's name and price.
Practice
(1/5)
1. What is the main purpose of computed properties in Vue's Composition API?
easy
A. To store static data that never changes
B. To define methods that run only once
C. To replace the data option in Options API
D. To create reactive values that update automatically when dependencies change
Solution
Step 1: Understand computed properties role
Computed properties are reactive and update automatically when their dependencies change.
Step 2: Compare with other options
Methods run on demand, static data doesn't update, and computed doesn't replace data but complements it.
Final Answer:
To create reactive values that update automatically when dependencies change -> Option D
Quick Check:
Computed = reactive auto-update [OK]
Hint: Computed = reactive values that auto-update [OK]
Common Mistakes:
Confusing computed with methods
Thinking computed stores static data
Believing computed replaces data option
2. Which of the following is the correct syntax to create a computed property named fullName inside setup()?
A. Missing return statement inside computed function
B. Refs should not be used inside computed
C. Computed must be declared outside setup()
D. firstName and lastName should not be refs
Solution
Step 1: Analyze computed function body
The function uses curly braces but does not return any value explicitly.
Step 2: Understand arrow function behavior
With curly braces, arrow functions need an explicit return; otherwise, they return undefined.
Final Answer:
Missing return statement inside computed function -> Option A
Quick Check:
Curly braces need return [OK]
Hint: Use return with curly braces in arrow functions [OK]
Common Mistakes:
Omitting return in arrow function with braces
Thinking refs can't be used in computed
Believing computed must be outside setup
5. You want to create a computed property statusMessage that returns "Active" if isActive is true, "Inactive" if false, and "Unknown" if isActive is null or undefined. Which code correctly implements this inside setup()?