📖 Scenario: You are building a simple Vue app that tracks a user's input text and automatically updates a message when the text changes.
🎯 Goal: Create a Vue component that uses a watch to react to changes in a reactive variable and update another variable as a side effect.
📋 What You'll Learn
Use Vue 3 Composition API with <script setup>
Create a reactive variable inputText initialized to an empty string
Create a reactive variable message initialized to 'Waiting for input...'
Use a watch to update message whenever inputText changes
Display both inputText and message in the template
💡 Why This Matters
🌍 Real World
Watching reactive data to trigger side effects is common in forms, live search, and dynamic UI updates.
💼 Career
Understanding watchers helps build responsive Vue apps that react to user input and data changes efficiently.
Progress0 / 4 steps
1
Set up reactive input variable
Inside the <script setup> block, import ref from 'vue' and create a reactive variable called inputText initialized to an empty string ''.
Vue
Hint
Use ref('') to create a reactive string variable called inputText.
2
Add reactive message variable
Below the inputText declaration, create another reactive variable called message initialized to the string 'Waiting for input...'.
Vue
Hint
Use ref('Waiting for input...') to create message.
3
Add watcher for inputText changes
Import watch from 'vue'. Use watch to watch the inputText variable. Inside the watcher callback, update message.value to `You typed: ${inputText.value}` whenever inputText changes.
Vue
Hint
Use watch(inputText, () => { ... }) and update message.value inside.
4
Complete template bindings
Ensure the template uses v-model="inputText" on the input field, and displays {{ inputText }} and {{ message }} inside <p> tags as shown.
Vue
Hint
Check that the input uses v-model="inputText" and both variables are shown in paragraphs.
Practice
(1/5)
1. What is the main purpose of a watch in Vue?
easy
A. To run code when reactive data changes
B. To define a new component
C. To style elements dynamically
D. To create a template for HTML
Solution
Step 1: Understand what watch does
A watcher observes reactive data and triggers code when that data changes.
Step 2: Compare options to watcher purpose
Only To run code when reactive data changes describes running code on reactive data change, which matches watcher behavior.
Final Answer:
To run code when reactive data changes -> Option A
Quick Check:
Watcher = run code on data change [OK]
Hint: Watchers react to data changes by running code [OK]
Common Mistakes:
Confusing watchers with components
Thinking watchers style elements
Assuming watchers create templates
2. Which of the following is the correct syntax to watch a reactive variable count in Vue 3 Composition API?
easy
A. watch(count, (newVal, oldVal) => { console.log(newVal) })
B. watch({ count }, (newVal, oldVal) => { console.log(newVal) })
C. watch('count', (newVal, oldVal) => { console.log(newVal) })
D. watch(() => count, (newVal, oldVal) => { console.log(newVal) })
Solution
Step 1: Recall Vue 3 watch syntax for reactive refs
In Vue 3 Composition API, to watch a ref's value, you typically pass a getter function like watch(() => count, callback) or watch(() => count.value, callback). Passing the ref directly watches the ref object, not its value.
Step 2: Check each option
watch(count, ...) watches the ref object, not its value. watch({ count }, ...) passes an object, invalid. watch('count', ...) uses invalid string. watch(() => count, ...) watches the value returned by the function, which is the ref's value.
B. Missing function to return count in watch source
C. Ref must be declared with reactive() instead of ref()
D. watch must be imported from 'vue-router'
Solution
Step 1: Check watch source argument
In Vue 3 Composition API, to watch a ref, you must pass a function returning the ref's value, like watch(() => count.value, callback).
Step 2: Identify the error
The code passes count.value directly, which is incorrect. It should be watch(() => count.value, ...).
Final Answer:
Missing function to return count in watch source -> Option B
Quick Check:
Watch source must be a function returning value [OK]
Hint: Watch source must be a function returning reactive value [OK]
Common Mistakes:
Passing count.value (primitive) directly instead of ref or getter
Thinking callback needs two parameters always
Importing watch from wrong package
5. You want to watch a reactive object user and run a side effect only when the user.name changes, ignoring other properties. Which watcher setup is correct?