📖 Scenario: You are building a simple Vue app that shows a user's full name by combining their first and last names. You want to decide whether to use a computed property or a watch effect to update the full name when the first or last name changes.
🎯 Goal: Create a Vue component that stores firstName and lastName as reactive variables. Then, use a computed property to automatically combine them into fullName. Finally, add a watch effect that logs a message whenever fullName changes.
📋 What You'll Learn
Use Vue 3.4+ Composition API with <script setup>
Create reactive variables firstName and lastName with initial values
Create a computed property called fullName that combines firstName and lastName
Use watch to log a message when fullName changes
Render the fullName in the template
💡 Why This Matters
🌍 Real World
Combining user input fields and showing derived data is common in forms, profiles, and settings pages.
💼 Career
Understanding <code>computed</code> and <code>watch</code> is essential for Vue developers to write efficient and reactive UI code.
Progress0 / 4 steps
1
Setup reactive firstName and lastName
In the <script setup> block, import ref from 'vue' and create two reactive variables called firstName with value 'John' and lastName with value 'Doe'.
Vue
Hint
Use ref to create reactive variables for firstName and lastName.
2
Add computed fullName property
Import computed from 'vue' and create a computed property called fullName that returns the combination of firstName.value and lastName.value separated by a space.
Vue
Hint
Use computed to create fullName that updates automatically when firstName or lastName change.
3
Add watch to log fullName changes
Import watch from 'vue' and add a watch effect on fullName that logs the message `Full name changed to: ${newValue}` whenever fullName changes.
Vue
Hint
Use watch to react to changes in fullName and log a message.
4
Complete template with input fields
Add two <input> elements bound with v-model to firstName and lastName inside the template so the user can change the names and see fullName update automatically.
Vue
Hint
Use v-model on inputs to bind to firstName and lastName. Add aria-label for accessibility.
Practice
(1/5)
1. Which Vue feature should you use to create a value that automatically updates and caches based on other reactive data?
easy
A. Computed property
B. Watch property
C. Method
D. Lifecycle hook
Solution
Step 1: Understand computed properties
Computed properties automatically update when their dependencies change and cache their results for efficiency.
Step 2: Compare with watch
Watch is for running code on data changes, not for caching reactive values.
Final Answer:
Computed property -> Option A
Quick Check:
Reactive cached value = Computed property [OK]
Hint: Use computed for reactive cached values, watch for side effects [OK]
Common Mistakes:
Using watch to compute and cache values
Confusing methods with computed properties
Thinking lifecycle hooks handle reactive caching
2. Which of the following is the correct syntax to define a computed property named fullName in Vue 3 Composition API using computed?
A. The watch source should be count not a function returning count
B. The watch source function should return count.value instead of count
C. The watch callback parameters are reversed
D. No error, code works correctly
Solution
Step 1: Understand watch source function
The watch source function should return the reactive value to watch, not the ref object itself.
Step 2: Identify the mistake
The function returns count (a ref), but it should return count.value to watch the actual value.
Final Answer:
The watch source function should return count.value instead of count -> Option B
Quick Check:
Watch source must return reactive value, not ref object [OK]
Hint: Return .value inside watch source function to track value changes [OK]
Common Mistakes:
Returning ref instead of its value in watch source
Confusing parameter order in watch callback
Assuming watch works without reactive source
5. You want to update the document title whenever the user's full name changes, which is computed from first and last names. Which Vue approach is best to implement this behavior?
hard
A. Use a computed property for full name and a watch on full name to update document title
B. Use only a computed property for full name and update document title inside it
C. Use a watch on first and last names separately to update document title
D. Use a method to compute full name and call it inside watch to update document title
Solution
Step 1: Separate display logic and side effects
Computed properties are ideal for reactive display values like full name, caching the result.
Step 2: Use watch for side effects
Updating document title is a side effect, so watch the computed full name to react to changes.
Final Answer:
Use a computed property for full name and a watch on full name to update document title -> Option A
Quick Check:
Computed for values, watch for side effects [OK]
Hint: Compute values with computed, react with watch for side effects [OK]
Common Mistakes:
Trying to update document title inside computed (not allowed)
Watching first and last names separately instead of computed
Using methods instead of computed for reactive values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.