Consider this Vue 3 component using a computed property with a getter and setter. What will be the displayed value after clicking the button once?
<script setup> import { ref, computed } from 'vue' const firstName = ref('John') const lastName = ref('Doe') const fullName = computed({ get() { return `${firstName.value} ${lastName.value}` }, set(value) { const parts = value.split(' ') firstName.value = parts[0] || '' lastName.value = parts[1] || '' } }) function updateName() { fullName.value = 'Jane Smith' } </script> <template> <div> <p>Full Name: {{ fullName }}</p> <button @click="updateName">Change Name</button> </div> </template>
Think about how the setter splits the string and updates the reactive refs.
The setter splits the string 'Jane Smith' into ['Jane', 'Smith'] and updates firstName and lastName. The getter then returns 'Jane Smith'.
Choose the correct way to define a computed property with both getter and setter functions.
Remember the computed function accepts an object with get and set keys for getter and setter.
Option A uses the correct keys get and set with function definitions. Option A is invalid because computed does not accept two functions as arguments. Option A uses wrong keys getter and setter. Option A uses arrow functions but the object keys are correct; however, arrow functions for get and set are allowed but less common; still, this syntax is valid in Vue 3, but the question expects the standard method syntax.
fullName after setting it to an empty string?Given this Vue 3 component, what will fullName.value be after calling fullName.value = ''?
<script setup> import { ref, computed } from 'vue' const firstName = ref('Alice') const lastName = ref('Wonderland') const fullName = computed({ get() { return `${firstName.value} ${lastName.value}`.trim() }, set(value) { const parts = value.split(' ') firstName.value = parts[0] || '' lastName.value = parts[1] || '' } }) fullName.value = '' </script>
Check how the setter updates firstName and lastName when given an empty string.
The setter splits the empty string into an array with one empty string element. So firstName.value becomes '', and lastName.value becomes ''. The getter returns the concatenation trimmed, which is an empty string.
Look at this Vue 3 code snippet. The setter is called but the displayed name does not update. What is the problem?
<script setup> import { ref, computed } from 'vue' const firstName = ref('Tom') const lastName = ref('Jerry') const fullName = computed({ get() { return `${firstName.value} ${lastName.value}` }, set(value) { const parts = value.split(' ') firstName.value = parts[0] || '' lastName.value = parts[1] || '' } }) function changeName() { fullName.value = 'Spike Bulldog' } </script> <template> <div> <p>{{ fullName }}</p> <button @click="changeName">Change</button> </div> </template>
Remember how to update reactive refs in Vue 3.
In the setter, firstName and lastName are reassigned to strings, breaking reactivity. Instead, their .value properties must be updated.
Which statement best explains how Vue 3 tracks reactive dependencies when using computed properties with both getter and setter?
Think about when Vue collects reactive dependencies for reactivity updates.
Vue collects reactive dependencies when the getter runs, so it knows when to update computed values. The setter is just a way to update reactive state but does not itself track dependencies.