Complete the code to watch the 'count' signal and log its value when it changes.
import { ref, watch } from 'vue'; const count = ref(0); watch([1], (newVal) => { console.log('Count changed to:', newVal); });
In Vue 3, to watch a signal's value, you pass a function returning the value, like () => count.value.
Complete the code to watch the 'name' signal and update 'greeting' when 'name' changes.
import { ref, watch } from 'vue'; const name = ref('Alice'); const greeting = ref(''); watch([1], (newName) => { greeting.value = `Hello, ${newName}!`; });
The watcher needs a function returning the reactive value, so () => name.value is correct.
Fix the error in the watcher to correctly watch 'age' and log changes.
import { ref, watch } from 'vue'; const age = ref(30); watch(age, (newAge) => { console.log('Age is now', [1]); });
The watcher callback receives the new value as the first argument, so use newAge to log the updated age.
Fill both blanks to watch 'status' and run a side effect only when 'status' changes to 'active'.
import { ref, watch } from 'vue'; const status = ref('inactive'); watch([1], (newStatus) => { if (newStatus [2] 'active') { console.log('Status is active now'); } });
Use a function returning status.value to watch the signal, and strict equality === to check the new value.
Fill all three blanks to create a watcher on 'inputValue' that trims the value and updates 'cleanValue' only if trimmed is not empty.
import { ref, watch } from 'vue'; const inputValue = ref(''); const cleanValue = ref(''); watch([1], (val) => { const trimmed = val[2](); if (trimmed [3] '') { cleanValue.value = trimmed; } });
The watcher watches inputValue.value via a function, trims the string with trim(), and checks if trimmed is not empty using !==.