Vue Academy · Lesson

Programmatic Navigation and useRouter

router.push(), router.replace(), router.go(), useRouter() and useRoute() composables.

Lesson 4 of 413 steps

Programmatic Navigation and useRouter is a free Vue Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, and your progress syncs across the web and the CoddyKit app. The Vue Academy course includes 4 lessons in total.

Navigating From Code

Besides RouterLink, you often need to navigate from JavaScript — after a form submit or login. The useRouter composable gives you the router instance to do this.

Getting the Router

Call useRouter() inside <script setup>. The returned object exposes push, replace, go, and more.

import { useRouter } from 'vue-router'
const router = useRouter()

Navigating With push

router.push adds an entry to the history stack — like clicking a link. The simplest form takes a path string.

router.push('/dashboard')

Push With a Route Object

You can also pass an object with name, params, and query. Named routes avoid hardcoding paths.

router.push({
  name: 'user',
  params: { id: 42 },
  query: { tab: 'profile' }
})

Replace: No History Entry

router.replace navigates without adding to history, so the back button skips it. Use it after login so users cannot go back to the login page.

router.replace('/dashboard')

Going Back and Forward

router.go(n) moves through history by n steps. router.go(-1) goes back one page; router.go(1) goes forward.

function goBack() {
  router.go(-1)
}

After an Async Action

A typical flow: submit a form, await the result, then navigate. push returns a Promise you can await for completion.

async function login() {
  await auth.signIn(form)
  router.push('/dashboard')
}

Reading the Current Route

To read params and query of the current route, use the useRoute composable. Do not confuse it with useRouter — useRoute is the route, useRouter is the navigator.

import { useRoute } from 'vue-router'
const route = useRoute()

Accessing Params

Read dynamic segments through route.params. For a route /user/:id, the id is at route.params.id.

const userId = route.params.id

Accessing Query Strings

Read the query string through route.query. For ?tab=profile, use route.query.tab.

const tab = route.query.tab

Reacting to Param Changes

Because route is reactive, watch it to respond when params change while staying on the same component.

import { watch } from 'vue'
watch(() => route.params.id, (newId) => {
  loadUser(newId)
})

Quick Check

Test your knowledge of programmatic navigation.

Recap

You learned programmatic navigation:

  • useRouter() gives push, replace, and go.
  • push adds history; replace does not; go(-1) goes back.
  • push accepts a path string or a { name, params, query } object.
  • useRoute() reads current params and query, and is reactive.
Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
42
Lessons
156

Frequently Asked Questions

Is the “Programmatic Navigation and useRouter” lesson free?

Yes — any 3 lessons of the Vue Academy learning path, including “Programmatic Navigation and useRouter”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Vue Academy course includes 4 lessons in total.

What will I learn in “Programmatic Navigation and useRouter”?

router.push(), router.replace(), router.go(), useRouter() and useRoute() composables. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Vue Academy?

No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Programmatic Navigation and useRouter” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Vue Academy lesson?

Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Route Meta Fields and Typing
  2. Route Transitions and Animation
  3. Scroll Behavior Configuration
  4. Programmatic Navigation and useRouter
← Back to Vue Academy