0Pricing
Vue Academy · Lesson

Setting Up Vue Router

Install Vue Router and define your first routes.

Setting Up Vue Router is a free Vue Academy lesson on CoddyKit — lesson 1 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Vue Router?

Vue Router is the official routing library for Vue.

  • Maps URLs to components
  • Enables single-page navigation without full page reloads
  • Manages history, links, and parameters

Installing Vue Router

Add Vue Router to your project with npm.

# Install Vue Router 4 (for Vue 3)
npm install vue-router

# Or have it added during npm create vue@latest

Creating Route Components

Each route renders a component. Start with simple page components.

<!-- Home.vue -->
<template>
  <h1>Home Page</h1>
</template>

<!-- About.vue -->
<template>
  <h1>About Page</h1>
</template>

Defining the Routes Array

A routes array maps paths to components.

import Home from "./views/Home.vue"
import About from "./views/About.vue"

const routes = [
  { path: "/", component: Home },
  { path: "/about", component: About }
]

createRouter

The createRouter function builds a router from the routes and a history mode.

import { createRouter, createWebHistory } from "vue-router"

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router

createWebHistory

createWebHistory uses the HTML5 history API for clean URLs like /about.

  • No hash in the URL
  • Requires server config so deep links work
  • The alternative is createWebHashHistory (/#/about)

Registering the Router

Install the router into the app with app.use.

import { createApp } from "vue"
import App from "./App.vue"
import router from "./router"

createApp(App)
  .use(router)
  .mount("#app")

router-view

The router-view component is where the matched route component renders.

<!-- App.vue -->
<template>
  <nav>...</nav>
  <router-view></router-view>
</template>

<!-- The current page renders here -->

router-link

Use router-link instead of plain anchor tags for navigation. It avoids full page reloads.

<template>
  <nav>
    <router-link to="/">Home</router-link>
    <router-link to="/about">About</router-link>
  </nav>
</template>

Active Link Styling

router-link automatically adds CSS classes to the active link.

  • router-link-active for partial matches
  • router-link-exact-active for exact matches

Style these classes to highlight the current page.

Putting It Together

The full flow:

  • Define routes mapping paths to components
  • Create the router with createRouter and createWebHistory
  • Register it with app.use(router)
  • Place router-view and navigate with router-link

Quick Check

Test your understanding of Vue Router setup.

Recap: Setting Up Vue Router

You set up client-side routing:

  • Install with npm install vue-router
  • Define a routes array
  • createRouter with createWebHistory
  • Register via app.use(router)
  • Use router-view and router-link

Next: nested and dynamic routes.

Frequently asked questions

Is the “Setting Up Vue Router” lesson free?

Yes — the full text of “Setting Up Vue Router” is free to read here on the web, and the Vue Academy course includes 3 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 “Setting Up Vue Router”?

Install Vue Router and define your first routes. 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 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Setting Up Vue Router” 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. Setting Up Vue Router
  2. Nested and Dynamic Routes
  3. Navigation Guards
← Back to Vue Academy