A computed property is a special reactive value that automatically updates when its dependencies change. It is used to calculate values based on other data in a Vue component.
Click to reveal answer
intermediate
How does a computed property with a getter and setter work in Vue?
It has two parts: a getter to return a value based on reactive data, and a setter to update reactive data when the computed property is changed.
Click to reveal answer
intermediate
Why use a setter in a computed property?
A setter lets you react to changes when someone modifies the computed property, allowing you to update the underlying data accordingly.
Click to reveal answer
beginner
Show a simple example of a computed property with getter and setter in Vue.
A. Arrow functions break the reactivity context for get and set
B. Missing commas between get and set
C. Cannot use computed with set method
D. Must use async functions for get and set
Solution
Step 1: Analyze arrow function usage
Arrow functions do not bind their own this, which can break reactivity when accessing reactive refs inside get/set.
Step 2: Understand Vue reactivity context
Using normal function syntax ensures correct reactivity and access to reactive data.
Final Answer:
Arrow functions break the reactivity context for get and set -> Option A
Quick Check:
Use normal functions, not arrow, for computed get/set [OK]
Hint: Avoid arrow functions in computed get/set [OK]
Common Mistakes:
Using arrow functions for get/set
Thinking commas are missing
Believing set is not allowed
5.
You want to create a computed property fullName that updates firstName and lastName refs, but also trims extra spaces and handles single names gracefully. Which computed property definition is best?