The component does not update userId when the route changes.
medium
A. The route object is not reactive, so computed won't update
B. The computed function is missing a dependency array
C. The useRoute hook must be called inside onMounted
D. The userId should be a ref, not computed
Solution
Step 1: Understand reactivity of route
The route object returned by useRoute() is reactive, but its params property is not deeply reactive by default.
Step 2: Why computed does not update
Because route.params is not reactive, the computed property does not track changes, so userId stays the same.
Final Answer:
The route object is not reactive, so computed won't update -> Option A
Quick Check:
Route params not reactive = A [OK]
Hint: Route params are not deeply reactive; watch route.fullPath instead [OK]
Common Mistakes:
Assuming route.params is reactive
Calling useRoute inside lifecycle hooks
Using ref instead of computed incorrectly
5. You want to create a Vue 3 component that displays the user ID from the route parameter and updates automatically when the route changes. Which approach correctly achieves this?
import { useRoute } from 'vue-router';
import { computed, watch, ref } from 'vue';
export default {
setup() {
const route = useRoute();
// What should userId be?
const userId = ???
return { userId };
}
}
hard
A. computed(() => route.params.id)
B. computed(() => route.params.id) with a watch on route.fullPath to trigger updates
C. computed(() => route.fullPath.split('/')[2])
D. ref(route.params.id) without any watcher
Solution
Step 1: Understand reactivity limitations
Route params are not deeply reactive, so computed(() => route.params.id) alone won't update when params change.
Step 2: Use a watch on route.fullPath to trigger updates
Watching route.fullPath (which is reactive) and recomputing userId ensures updates when the route changes.
Final Answer:
computed(() => route.params.id) with a watch on route.fullPath to trigger updates -> Option B
Quick Check:
Watch route.fullPath for param changes = C [OK]
Hint: Watch route.fullPath to react to param changes in computed [OK]
Common Mistakes:
Using computed alone without watch
Parsing fullPath manually without reactivity
Using ref without updating on route change
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.