Programmatic navigation lets you change pages in your app using code, not just links. This helps when you want to move users automatically after actions like login or form submission.
Programmatic navigation with useRouter in Vue
import { useRouter } from 'vue-router' const router = useRouter() // To navigate to a new route router.push('/path') // Or with route name and params router.push({ name: 'routeName', params: { id: 123 } })
useRouter is a function from vue-router that gives you access to navigation methods.
router.push() changes the page programmatically.
router.push('/home')router.push({ name: 'profile', params: { userId: 42 } })router.push({ path: '/about' })This Vue component shows a button. When clicked, it uses useRouter to navigate to the '/dashboard' page programmatically.
<script setup> import { useRouter } from 'vue-router' const router = useRouter() function goToDashboard() { router.push('/dashboard') } </script> <template> <button @click="goToDashboard" aria-label="Go to Dashboard">Go to Dashboard</button> </template>
Always import useRouter from vue-router before using it.
Programmatic navigation does not reload the page; it changes the route inside your app.
Use router.push() for normal navigation and router.replace() if you want to replace the current history entry.
Programmatic navigation lets you move users between pages using code.
useRouter provides the router.push() method to change routes.
This is useful for redirects after actions like login or form submission.
Practice
useRouter function provide in a Vue 3 component?Solution
Step 1: Understand the purpose of
useRouteruseRouteris a Vue Router composable that gives access to the router instance inside setup functions.Step 2: Identify what the router instance allows
The router instance allows programmatic navigation, such as usingrouter.push()to change routes.Final Answer:
Access to the router instance to navigate programmatically -> Option CQuick Check:
useRouter= router access [OK]
- Thinking useRouter fetches data
- Confusing useRouter with state management
- Assuming useRouter styles components
useRouter in Vue 3?Solution
Step 1: Recall the method to change routes programmatically
The correct method to navigate isrouter.push().Step 2: Match the syntax with the correct method
Onlyrouter.push('/home')is valid; other methods likego,navigate, orchangedo not exist on the router instance.Final Answer:
const router = useRouter(); router.push('/home'); -> Option BQuick Check:
router.push() navigates routes [OK]
- Using router.go() instead of router.push()
- Trying router.navigate() which is invalid
- Calling router.change() which doesn't exist
import { useRouter } from 'vue-router';
export default {
setup() {
const router = useRouter();
function goToProfile() {
router.push({ name: 'profile', params: { userId: 42 } });
}
return { goToProfile };
}
}What happens when
goToProfile() is called?Solution
Step 1: Understand router.push with named routes and params
Usingrouter.push({ name: 'profile', params: { userId: 42 } })navigates to the route named 'profile' and passes the parameter userId=42.Step 2: Confirm this is valid syntax and behavior
This is the correct way to navigate to a named route with parameters in Vue Router.Final Answer:
The app navigates to the route named 'profile' with userId 42 as a parameter -> Option DQuick Check:
router.push({name, params}) navigates with params [OK]
- Thinking params can't be passed as object
- Assuming router.push only accepts string paths
- Confusing params with query parameters
useRouter:import { useRouter } from 'vue-router';
export default {
setup() {
const router = useRouter;
function goHome() {
router.push('/home');
}
return { goHome };
}
}Solution
Step 1: Check how useRouter is assigned
The code assignsconst router = useRouter;without parentheses, so router is a reference to the function, not the router instance.Step 2: Understand the impact on router.push call
Since router is not the router instance, callingrouter.push()will cause an error because router is not an object with push method.Final Answer:
useRouter is not called as a function, missing parentheses -> Option AQuick Check:
Call useRouter() with () to get router instance [OK]
- Forgetting parentheses after useRouter
- Using string instead of object incorrectly (not error here)
- Not returning functions from setup (actually returned here)
useRouter for this programmatic navigation?Solution
Step 1: Confirm correct useRouter call
const router = useRouter(); function checkAuth() { if (!isAuthenticated) router.push('/login'); } correctly callsuseRouter()with parentheses to get the router instance.Step 2: Verify correct navigation method
const router = useRouter(); function checkAuth() { if (!isAuthenticated) router.push('/login'); } usesrouter.push('/login'), which is the correct method to navigate programmatically.Final Answer:
const router = useRouter(); function checkAuth() { if (!isAuthenticated) router.push('/login'); } -> Option AQuick Check:
Call useRouter() and use router.push() for navigation [OK]
- Not calling useRouter as a function
- Using router.go() or router.navigate() which don't exist
- Forgetting to check authentication before navigation
Ready to prove it?
Pass the test and this pattern moves unknown
