Programmatic Navigation and useRouter
router.push(), router.replace(), router.go(), useRouter() and useRoute() composables.
Programmatic Navigation and useRouter is a free Vue Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.idAccessing Query Strings
Read the query string through route.query. For ?tab=profile, use route.query.tab.
const tab = route.query.tabReacting 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.
Frequently asked questions
Is the “Programmatic Navigation and useRouter” lesson free?
Yes — the full text of “Programmatic Navigation and useRouter” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
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
- Route Meta Fields and Typing
- Route Transitions and Animation
- Scroll Behavior Configuration
- Programmatic Navigation and useRouter