Consider this Vue 3 component using useRoute to access route parameters.
import { defineComponent } from 'vue';
import { useRoute } from 'vue-router';
export default defineComponent({
setup() {
const route = useRoute();
return { id: route.params.id };
},
template: `User ID: {{ id }}`
});If the current route is /user/42 where id is a route param, what will be rendered?
import { defineComponent } from 'vue'; import { useRoute } from 'vue-router'; export default defineComponent({ setup() { const route = useRoute(); return { id: route.params.id }; }, template: `<div>User ID: {{ id }}</div>` });
Remember that useRoute() gives access to the current route's parameters.
The useRoute() hook returns the current route object. Accessing route.params.id retrieves the 'id' parameter from the URL. If the URL is /user/42, then id is '42', so the component renders User ID: 42.
Given a nested route with parameter postId, which code correctly extracts it inside setup()?
import { useRoute } from 'vue-router'; export default { setup() { // Which option correctly gets postId? } }
Remember useRoute is a function that returns the route object.
Option C correctly calls useRoute() to get the route object, then accesses params.postId. Option C tries to access params on the function itself, which is invalid. Option C calls useRoute() inline, which is valid but less clear. Option C assigns the function itself without calling it, so route.params is undefined.
Consider this Vue 3 component that uses useRoute and a watch to update userId:
import { ref, watch } from 'vue';
import { useRoute } from 'vue-router';
export default {
setup() {
const route = useRoute();
const userId = ref(route.params.id);
watch(() => route.params.id, (newId) => {
userId.value = newId;
});
return { userId };
}
};If the route changes from /user/1 to /user/5, what will be the value of userId.value?
import { ref, watch } from 'vue'; import { useRoute } from 'vue-router'; export default { setup() { const route = useRoute(); const userId = ref(route.params.id); watch(() => route.params.id, (newId) => { userId.value = newId; }); return { userId }; } };
The watch updates userId when the route param changes.
The watch listens for changes to route.params.id. When the route changes from '/user/1' to '/user/5', the watcher runs and updates userId.value to '5'.
Look at this Vue 3 component:
import { defineComponent } from 'vue';
import { useRoute } from 'vue-router';
export default defineComponent({
setup() {
const route = useRoute;
return { productId: route.params.productId };
},
template: `Product: {{ productId }}`
});When the route is /product/10, the component shows Product: undefined. Why?
import { defineComponent } from 'vue'; import { useRoute } from 'vue-router'; export default defineComponent({ setup() { const route = useRoute; return { productId: route.params.productId }; }, template: `<div>Product: {{ productId }}</div>` });
Check how useRoute is used in setup().
The code assigns route = useRoute without calling it. So route is the function, not the route object. Accessing route.params is undefined, causing productId to be undefined.
useRoute and reactive route params is true?Consider the following statements about useRoute in Vue Router:
useRoute()returns a reactive route object.- Route params accessed via
useRoute().paramsare reactive and update automatically. - Directly destructuring params like
const { id } = useRoute().paramskeepsidreactive. - Watching
useRoute().params.idis the recommended way to react to param changes.
Which is the only true statement?
Think about how destructuring affects reactivity in Vue.
useRoute() returns a reactive route object, so statements 1 and 2 are true. However, destructuring const { id } = useRoute().params breaks reactivity because it copies the value, so statement 3 is false. Watching useRoute().params.id is the recommended way to react to param changes, so statement 4 is true. But since statement 4 is true and 3 is false, only 1 and 2 are fully true together.