0
0
Vueframework~10 mins

Programmatic navigation with useRouter in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Programmatic navigation with useRouter
Import useRouter
Call useRouter() to get router
Define function to navigate
Call router.push() with target path
Vue Router changes URL and view
Component updates to new route
This flow shows how to import and use useRouter to navigate programmatically by calling router.push() to change the route and update the view.
Execution Sample
Vue
import { useRouter } from 'vue-router'

export default {
  setup() {
    const router = useRouter()

    function goHome() {
      router.push('/')
    }

    return { goHome }
  }
}
This code imports useRouter, gets the router instance inside setup, and defines a function to navigate to the home page.
Execution Table
StepActionEvaluationResult
1Import useRouter from vue-routeruseRouter is availableReady to get router instance
2Call useRouter()router = useRouter()router object obtained
3Define goHome functionfunction goHome() {...}Function ready to call
4Call goHome()router.push('/')Navigation to '/' triggered
5Vue Router updates URLURL changes to '/'View updates to home page
6Component re-rendersNew route activeHome page content displayed
💡 Navigation completes when router.push updates the URL and Vue updates the view.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
routerundefinedrouter objectrouter objectrouter object
Key Moments - 2 Insights
Why do we call useRouter() inside the setup function or component?
useRouter() must be called inside the component's setup or lifecycle because it accesses Vue Router's context. See execution_table step 2 where router is obtained.
What happens if we call router.push() with a wrong path?
Vue Router tries to navigate but shows a 404 or fallback route if path is invalid. The navigation still triggers as in step 4 but view updates differently.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'router' after step 2?
Aundefined
Brouter object
Cfunction goHome
Dnull
💡 Hint
Check the variable_tracker table column 'After Step 2' for 'router'
At which step does the URL actually change to '/'?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
See execution_table step 5 where URL changes
If we call router.push('/about') instead of '/', which step changes in the execution table?
AStep 6 component re-render
BStep 2 router creation
CStep 4 action and evaluation
DStep 1 import
💡 Hint
Step 4 shows the navigation target in action and evaluation columns
Concept Snapshot
Programmatic navigation with useRouter:
- Import useRouter from 'vue-router'
- Call useRouter() inside setup to get router
- Use router.push('/path') to navigate
- Vue Router updates URL and view
- Component re-renders with new route content
Full Transcript
Programmatic navigation in Vue uses the useRouter function from vue-router. First, import useRouter and call it inside your component's setup function to get the router instance. Then define a function that calls router.push() with the target path string. When you call this function, Vue Router changes the URL and updates the view to the new route. The component re-renders to show the new page content. This lets you navigate without user clicks on links, useful for buttons or after actions.