Thuta Learning
Vue.js
AdvancedWeb DevelopmentBeginner

Routing with Vue Router

Vue.jsLesson 1725 min

What you'll walk away with

  • Build a route table
  • Use RouterLink and RouterView
  • Understand dynamic routes

Explain it simply

Routing with Vue Router, in plain language

Vue Router is Vue's official client-side routing solution. createRouter maps URL paths to components, and RouterView renders whichever component matches. RouterLink updates the browser history without triggering a full page reload.

Problem → Why → Solution

The problem — why this concept matters

Forgetting to call app.use(router)

A common mistake

Mixing up route params and path when calling router.push

Core concept — understand how it works

Build single-page application navigation with Vue Router, switching views without a full page reload.

Mental model
  1. URL or action
  2. Vue Router
  3. component view

Build a route table

Core syntax or pattern

javascript
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from './views/HomeView.vue'
import CourseView from './views/CourseView.vue'

export const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', name: 'home', component: HomeView },
    { path: '/courses/:slug', name: 'course', component: CourseView, props: true }
  ]
})

Build single-page application navigation with Vue Router, switching views without a full page reload.

Read the code step by step

  1. URL or actionimport { createRouter, createWebHistory } from 'vue-r...

    Vue Router is Vue's official client-side routing solution. createRouter maps URL paths to components, and RouterView renders whichever component matches. RouterLink updates the browser history without triggering a full page reload.

  2. Vue Routerroutes: [

    Build single-page application navigation with Vue Router, switching views without a full page reload.

  3. component view})

    Apply Routing with Vue Router in a small example and test normal, edge, and failure cases.

Execution flow
  1. URL or action
  2. Vue Router
  3. component view

Success

Build a route table

Failure

Forgetting to call app.use(router)

Let's break it down simply

Vue Router is Vue's official client-side routing solution. createRouter maps URL paths to components, and RouterView renders whichever component matches. RouterLink updates the browser history without triggering a full page reload.

javascript
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from './views/HomeView.vue'
import CourseView from './views/CourseView.vue'

export const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', name: 'home', component: HomeView },
    { path: '/courses/:slug', name: 'course', component: CourseView, props: true }
  ]
})
You should see
/ → HomeView
/courses/vue → CourseView (slug: vue)

Try it yourself

Build three routes — Home, Courses, and Course Detail — and add navigation links.

Vue Router — Getting Started — Vue Router

Real-world usage

How to apply it in a real project

Apply Routing with Vue Router in a small example and test normal, edge, and failure cases.

Mini challenge

Build it yourself

Build three routes — Home, Courses, and Course Detail — and add navigation links.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
javascript
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from './views/HomeView.vue'
import CourseView from './views/CourseView.vue'

export const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', name: 'home', component: HomeView },
    { path: '/courses/:slug', name: 'course', component: CourseView, props: true }
  ]
})

Expected: Build a route table

Show hint

Apply Routing with Vue Router in a small example and test normal, edge, and failure cases.

Quick quiz 1 / 3

What best describes the core idea of Routing with Vue Router?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • URL or action → Vue Router → component view
  • Build single-page application navigation with Vue Router, switching views without a full page reload.
  • Forgetting to call app.use(router)
  • Apply Routing with Vue Router in a small example and test normal, edge, and failure cases.

Vue Router — Getting Started — Vue Router

Easy traps

  • Forgetting to call app.use(router)
  • Mixing up route params and path when calling router.push

Exercise

Build three routes — Home, Courses, and Course Detail — and add navigation links.

You'll know it worked when: / → HomeView /courses/vue → CourseView (slug: vue)

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Routing with Vue Router | Thuta Learning