A computed property is a special function that automatically calculates and updates a value based on reactive data. It caches the result and only recalculates when its dependencies change.
Click to reveal answer
beginner
Why use computed properties instead of methods for derived state?
Computed properties cache their results and only update when dependencies change, making them more efficient than methods, which run every time they are called.
Click to reveal answer
intermediate
How do you define a computed property using the Composition API in Vue 3?
You import the 'computed' function from 'vue' and pass a function that returns the derived value. Example: const fullName = computed(() => firstName.value + ' ' + lastName.value);
Click to reveal answer
beginner
What happens if a computed property depends on reactive data that changes?
The computed property automatically recalculates its value and updates any parts of the UI that use it, keeping the interface in sync with the data.
Click to reveal answer
intermediate
Can computed properties have setters in Vue? What is their use?
Yes, computed properties can have setters to allow two-way binding. The setter lets you update the underlying reactive data when the computed property is assigned a new value.
Click to reveal answer
What does a computed property in Vue do?
ACalculates a value and caches it until dependencies change
BRuns every time the component renders without caching
CDirectly modifies the DOM elements
DStores static data that never changes
✗ Incorrect
Computed properties calculate values based on reactive data and cache the result until dependencies change.
Which Vue API is used to create a computed property in Vue 3 Composition API?
Areactive()
Bref()
Ccomputed()
Dwatch()
✗ Incorrect
The computed() function creates computed properties that derive values from reactive data.
When does a computed property recalculate its value?
AWhen one of its reactive dependencies changes
BEvery time the component renders
COnly when the component mounts
DWhen the user clicks a button
✗ Incorrect
Computed properties recalculate only when their reactive dependencies change, improving efficiency.
What is the benefit of using a computed property over a method for derived state?
AComputed properties can modify data directly
BMethods automatically cache results
CMethods are faster than computed properties
DComputed properties cache results and avoid unnecessary recalculations
✗ Incorrect
Computed properties cache their results and only update when dependencies change, unlike methods.
Can computed properties in Vue have setters?
ANo, they are read-only
BYes, to allow two-way data binding
COnly in Vue 2
DOnly if you use methods
✗ Incorrect
Computed properties can have setters to update reactive data, enabling two-way binding.
Explain how computed properties help keep the UI in sync with data in Vue.
Think about how Vue tracks changes and updates the screen.
You got /4 concepts.
Describe how to create a computed property using Vue 3 Composition API and why it is preferred over methods for derived state.
Focus on the syntax and benefits of computed properties.
You got /4 concepts.
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.