Performance: Programmatic navigation with useRouter
This affects page navigation speed and user interaction responsiveness when changing routes programmatically.
const router = useRouter(); function goToPage() { router.push('/page2'); }
const router = useRouter(); function goToPage() { router.push('/page1'); router.push('/page2'); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple rapid router.push calls | High (many DOM updates) | Multiple reflows | High paint cost | [X] Bad |
| Single router.push call | Low (one DOM update) | Single reflow | Low paint cost | [OK] Good |
useRouter function provide in a Vue 3 component?useRouteruseRouter is a Vue Router composable that gives access to the router instance inside setup functions.router.push() to change routes.useRouter = router access [OK]useRouter in Vue 3?router.push().router.push('/home') is valid; other methods like go, navigate, or change do not exist on the router instance.import { useRouter } from 'vue-router';
export default {
setup() {
const router = useRouter();
function goToProfile() {
router.push({ name: 'profile', params: { userId: 42 } });
}
return { goToProfile };
}
}goToProfile() is called?router.push({ name: 'profile', params: { userId: 42 } }) navigates to the route named 'profile' and passes the parameter userId=42.useRouter:import { useRouter } from 'vue-router';
export default {
setup() {
const router = useRouter;
function goHome() {
router.push('/home');
}
return { goHome };
}
}const router = useRouter; without parentheses, so router is a reference to the function, not the router instance.router.push() will cause an error because router is not an object with push method.useRouter for this programmatic navigation?useRouter() with parentheses to get the router instance.router.push('/login'), which is the correct method to navigate programmatically.Pass the test and this pattern moves unknown