What is a computed property in Vue's Composition API?
A computed property is a special reactive value that automatically updates when its dependencies change. It caches its result until those dependencies change again.
Click to reveal answer
beginner
How do you create a computed property in Vue's Composition API?
You import <code>computed</code> from 'vue' and call it with a function that returns the value you want to compute. Example: <code>const double = computed(() => count.value * 2)</code>.
Click to reveal answer
intermediate
Why use computed instead of a regular function in Vue's Composition API?
Because computed caches its result and only recalculates when dependencies change, making your app more efficient and reactive.
Click to reveal answer
intermediate
What happens if you try to assign a value directly to a computed property without a setter?
You will get an error because computed properties without a setter are read-only. To assign values, you must define a setter function.
Click to reveal answer
advanced
Show a simple example of a writable computed property in Vue's Composition API.
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()?