Skip to content

Chapter 8 of 8

Routing with Vue Router

Vue Router is the official routing library for Vue.js, and it enables single-page application navigation by mapping URL paths to components. It supports nested routes for hierarchical layouts, dynamic route matching with parameters, named routes and views, and navigation guards for controlling access. The library can operate in HTML5 history mode, which produces clean URLs, or in hash mode, which is easier to deploy to static hosts that lack server-side rewrite support.

Routes are defined as an array of objects, each specifying a path and a component. Dynamic segments are introduced with a colon, so a path like /user/:id matches URLs such as /user/42 and exposes the value as route.params.id. A catch-all route such as /:pathMatch(.*)* is typically used to render a 404 page for unmatched URLs. The router instance is created with createRouter({ history: createWebHistory(), routes }) and then passed to the Vue application via app.use(router).

Programmatic navigation is performed through methods on the router instance. Calling router.push('/about') navigates to a new URL while adding an entry to the browser history, router.replace('/login') does the same without leaving a history trace, and router.go(-1) moves backward or forward through the history stack. Named routes can be targeted by passing an object like { name: 'user', params: { id: 1 } }. Navigation guards add control over when these transitions occur: beforeEach runs globally before every navigation, beforeEnter is scoped to a specific route, and beforeRouteEnter and beforeRouteLeave are defined inside components. They are commonly used for authentication checks, fetching data before a route is entered, or preventing navigation away from a page with unsaved changes.

All chapters
  1. 1Introduction to Vue and the Reactivity System
  2. 2Component API Styles: Options vs Composition
  3. 3Template Directives
  4. 4Computed Properties and Watchers
  5. 5Component Lifecycle, Props, and Emits
  6. 6Slots, Provide/Inject, and Advanced Features
  7. 7State Management with Pinia
  8. 8Routing with Vue Router

Drill it

Reading is not remembering. These come from the Vue Framework deck:

Q

What is Vue.js?

Vue.js is a progressive JavaScript framework for building user interfaces. It is designed to be incrementally adoptable and focuses on the view layer, making it...

Q

What is the Vue reactivity system?

The Vue reactivity system automatically tracks dependencies and updates the DOM when reactive state changes. In Vue 3, it uses Proxy objects (instead of Object....

Q

How do you create a reactive object in Vue 3?

Use reactive() from the Composition API:import { reactive } from 'vue'const state = reactive({ count: 0, name: 'Vue' })This returns a deeply reactive proxy of t...

Q

What is the difference between ref() and reactive()?

ref() wraps a single value (primitive or object) and requires .value to access it in script. reactive() wraps an object and provides direct property access.cons...