0
0
Vueframework~5 mins

Computed in Composition API in Vue - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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(() =&gt; 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: () =&gt; firstName.value + ' ' + lastName.value,
  set: (val) =&gt; {
    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?
ACaches a value and updates only when dependencies change
BRuns a function every time the component renders
CStores a static value that never changes
DDirectly modifies reactive state
How do you import the computed function in Vue 3 Composition API?
Aimport computed from 'vue3'
Bimport computed from 'vue-computed'
Cimport { Computed } from 'vue'
Dimport { computed } from 'vue'
What will happen if you try to assign a value to a read-only computed property?
AIt will update the value silently
BIt will throw an error
CIt will ignore the assignment
DIt will convert to a reactive ref
Which of these is a correct way to define a computed property?
Aconst total = computed(() =&gt; price.value + tax.value)
Bconst total = computed(price.value + tax.value)
Cconst total = computed = () =&gt; price + tax
Dconst total = computed { return price + tax }
What is the main benefit of using computed over a method in Vue?
AMethods are reactive but computed are not
BMethods automatically update when dependencies change
CComputed properties cache results for better performance
DComputed properties can only be used in templates
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.