Consider this Vue 3 component using useRouter to navigate:
import { useRouter } from 'vue-router';
import { defineComponent } from 'vue';
export default defineComponent({
setup() {
const router = useRouter();
function goHome() {
router.push('/home');
}
return { goHome };
}
});What is the result when goHome() is called?
import { useRouter } from 'vue-router'; import { defineComponent } from 'vue'; export default defineComponent({ setup() { const router = useRouter(); function goHome() { router.push('/home'); } return { goHome }; } });
Think about how Vue Router handles navigation programmatically.
Calling router.push('/home') changes the URL and navigates to the new route without reloading the page. This is the standard SPA navigation behavior.
Given a route named 'user' that expects a param id, which code correctly navigates to user with id 42?
Named routes use name and params keys.
To navigate to a named route with params, use router.push({ name: 'user', params: { id: 42 } }). Using query or path with params won't work as expected.
Look at this code snippet inside a Vue component:
const router = useRouter();
function goProfile() {
router.push('/profile');
}
// Called on button click
But clicking the button does not change the URL or route. Why?
const router = useRouter(); function goProfile() { router.push('/profile'); } // Called on button click <button @click="goProfile">Go</button>
Check if the function is accessible in the template.
If goProfile is defined inside setup() but not returned, the template cannot call it, so clicking the button does nothing.
Given this code inside a Vue 3 component:
import { useRouter, useRoute } from 'vue-router';
const router = useRouter();
const currentRoute = useRoute();
await router.push('/dashboard');
console.log(currentRoute.path);What will be logged?
import { useRouter, useRoute } from 'vue-router'; const router = useRouter(); const currentRoute = useRoute(); await router.push('/dashboard'); console.log(currentRoute.path);
Think about reactivity and when currentRoute updates.
useRoute() returns a reactive object. Awaiting router.push() ensures navigation completes, so currentRoute.path is '/dashboard'.
Choose the correct statement about useRouter and programmatic navigation in Vue 3.
Think about where useRouter is designed to be used.
useRouter is a Vue Router composable meant to be used inside the setup() function or lifecycle hooks of Vue components. It returns the router instance used by the app. It does not create new instances. Calling router.push() changes the URL without reloading the page.