Recall & Review
beginner
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.
Example:
<pre>const fullName = computed({
get: () => firstName.value + ' ' + lastName.value,
set: (val) => {
const parts = val.split(' ')
firstName.value = parts[0]
lastName.value = parts[1] || ''
}
})</pre>Click to reveal answer
What does a
computed property in Vue's Composition API do?✗ Incorrect
Computed properties cache their result and only update when their reactive dependencies change.
How do you import the
computed function in Vue 3 Composition API?✗ Incorrect
The correct import is
import { computed } from 'vue'.What will happen if you try to assign a value to a read-only computed property?
✗ Incorrect
Read-only computed properties throw an error if you try to assign a value to them.
Which of these is a correct way to define a computed property?
✗ Incorrect
Computed properties must be created by calling
computed with a function.What is the main benefit of using
computed over a method in Vue?✗ Incorrect
Computed properties cache their results and only recompute when dependencies change, improving performance.
Explain how to create and use a computed property in Vue's Composition API.
Think about how you make a reactive value that updates automatically.
You got /4 concepts.
Describe the difference between a computed property and a method in Vue's Composition API.
Consider performance and when values are recalculated.
You got /4 concepts.