📖 Scenario: You are building a simple Vue 3 app that shows a list of products with their prices. You want to display the total price of all products dynamically.
🎯 Goal: Create a Vue 3 component that uses a computed property to calculate the total price from a list of products.
📋 What You'll Learn
Create a reactive list of products with name and price
Add a reactive variable for a discount percentage
Use a computed property to calculate the total price after discount
Display the product names, prices, and the discounted total price
💡 Why This Matters
🌍 Real World
Calculating totals and discounts is common in shopping carts and sales dashboards.
💼 Career
Understanding computed properties is essential for building efficient, reactive Vue applications that update UI automatically.
Progress0 / 4 steps
1
Setup the products list
Create a reactive variable called products using ref that holds an array of objects. Each object should have name and price properties with these exact values: { name: 'Apple', price: 100 }, { name: 'Banana', price: 50 }, and { name: 'Cherry', price: 75 }.
Vue
Hint
Use ref from Vue to create a reactive array named products with the exact objects inside.
2
Add a discount percentage
Create a reactive variable called discount using ref and set it to 10 to represent a 10% discount.
Vue
Hint
Use ref to create a reactive variable named discount and set it to 10.
3
Create a computed property for total price after discount
Import computed from 'vue'. Create a computed property called totalPrice that calculates the sum of all product prices from products.value and then applies the discount percentage from discount.value. Use this formula: sum of prices * (1 - discount / 100).
Vue
Hint
Use computed to create totalPrice. Use reduce on products.value to sum prices, then apply the discount.
4
Display products and total price in template
In the <template>, use a v-for directive to list each product's name and price. Below the list, display the text Total Price after discount: followed by the totalPrice value formatted with toFixed(2).
Vue
Hint
Use v-for to loop over products and show name and price. Then show totalPrice.toFixed(2) below.
Practice
(1/5)
1. What is the main purpose of a computed property in Vue?
easy
A. To directly modify the DOM elements
B. To store static data that never changes
C. To create a value that updates automatically when its dependencies change
D. To replace methods for event handling
Solution
Step 1: Understand computed property role
Computed properties automatically update when their reactive dependencies change, unlike static data.
Step 2: Compare with other options
Static data does not update automatically, DOM manipulation is done differently, and event handling uses methods or event listeners.
Final Answer:
To create a value that updates automatically when its dependencies change -> Option C
Quick Check:
Computed properties = auto-updating values [OK]
Hint: Computed properties auto-update when data changes [OK]
Common Mistakes:
Thinking computed properties store static data
Confusing computed with methods for events
Assuming computed directly changes the DOM
2. Which of the following is the correct syntax to define a computed property named fullName using Vue's Composition API?
A. Missing return statement inside computed function
B. count should not be a ref
C. computed should be imported from 'vuex'
D. doubleCount should be a method, not computed
Solution
Step 1: Check computed function body
The arrow function uses braces but does not return a value explicitly, so it returns undefined.
Step 2: Correct usage
Either remove braces or add a return statement to return count.value + 2.
Final Answer:
Missing return statement inside computed function -> Option A
Quick Check:
Computed with braces needs return [OK]
Hint: Add return when using braces in arrow function [OK]
Common Mistakes:
Forgetting return in block arrow functions
Importing computed from wrong package
Misusing ref as normal variable
5. You have a list of products with prices and quantities as refs. How would you create a computed property totalPrice that sums the product of price and quantity for all products?
Choose the correct computed property code snippet:
hard
A. const totalPrice = computed(() => products.reduce((sum, p) => sum + p.price * p.quantity, 0))
B. const totalPrice = computed(() => products.value.reduce((sum, p) => sum + p.price.value * p.quantity.value, 0))
C. const totalPrice = computed(() => { products.value.forEach(p => sum += p.price.value * p.quantity.value); return sum })
Each product has price and quantity as refs, so access with .value. products is a ref array, so use products.value.
Step 2: Choose correct aggregation method
Use reduce to sum all price * quantity values. const totalPrice = computed(() => products.value.reduce((sum, p) => sum + p.price.value * p.quantity.value, 0)) correctly uses reduce with .value access and initial sum 0.
Step 3: Analyze incorrect options
const totalPrice = computed(() => products.reduce((sum, p) => sum + p.price * p.quantity, 0)) misses .value on refs. const totalPrice = computed(() => { products.value.forEach(p => sum += p.price.value * p.quantity.value); return sum }) uses forEach but sum is undefined. const totalPrice = computed(() => products.value.map(p => p.price.value * p.quantity.value)) returns array, not sum.
Final Answer:
const totalPrice = computed(() => products.value.reduce((sum, p) => sum + p.price.value * p.quantity.value, 0)) -> Option B
Quick Check:
Use reduce with .value refs to sum totals [OK]
Hint: Use reduce with .value to sum reactive refs [OK]
Common Mistakes:
Forgetting .value on refs inside products
Using map instead of reduce for sum
Not initializing sum in reduce
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.