Vue Router

5.0.4 · active · verified Sun Apr 19

Vue Router is the official client-side routing library for Vue.js, providing robust and declarative navigation for single-page applications. The current stable version is 5.0.4, primarily designed for Vue 3 projects. It generally follows a regular release cadence, with minor bug fixes and experimental features being integrated frequently, and major versions released less often but incorporating significant architectural changes or merges. A key differentiator of Vue Router 5 is the integration of `unplugin-vue-router` into its core, enabling file-system based routing and simplifying route definition by convention. This merge aims to streamline development workflows, reducing boilerplate compared to earlier versions and offering a more integrated experience for large-scale applications. It also provides strong TypeScript support out of the box, ensuring type safety for route definitions and navigation guards.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic setup of Vue Router 5 with Vue 3, including route definitions, history mode, `RouterLink`, and `RouterView` components, and a catch-all 404 route.

import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import App from './App.vue'

// 1. Define route components.
const Home = { template: '<div>Home Page</div>' }
const About = { template: '<div>About Page</div>' }
const User = { 
  template: `
    <div>
      User {{ $route.params.id }}
      <RouterLink :to="'/user/' + ($route.params.id as any) + '/profile'">Profile</RouterLink>
      <RouterLink :to="'/user/' + ($route.params.id as any) + '/posts'">Posts</RouterLink>
      <RouterView />
    </div>
  ` 
}
const UserProfile = { template: '<div>User Profile</div>' }
const UserPosts = { template: '<div>User Posts</div>' }
const NotFound = { template: '<div>404 Not Found</div>' }

// 2. Define some routes
// Each route should map to a component.
const routes = [
  { path: '/', component: Home },
  { path: '/about', component: About },
  { 
    path: '/user/:id',
    component: User,
    children: [
      { path: 'profile', component: UserProfile },
      { path: 'posts', component: UserPosts },
    ]
  },
  { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound },
]

// 3. Create the router instance and pass the `routes` option
const router = createRouter({
  // 4. Provide the history implementation to use.
  history: createWebHistory(process.env.BASE_URL ?? '/'),
  routes, // short for `routes: routes`
})

// 5. Create and mount the root instance.
const app = createApp(App)
app.use(router)

app.mount('#app')

/* In your App.vue template: */
// <template>
//   <h1>Hello Vue Router!</h1>
//   <nav>
//     <RouterLink to="/">Go to Home</RouterLink>
//     <RouterLink to="/about">Go to About</RouterLink>
//     <RouterLink to="/user/123">Go to User 123</RouterLink>
//     <RouterLink to="/non-existent">Go to 404</RouterLink>
//   </nav>
//   <main>
//     <RouterView />
//   </main>
// </template>

// <script setup lang="ts">
// import { RouterLink, RouterView } from 'vue-router'
// </script>

view raw JSON →