Registry / web-framework / vue-router

vue-router

JSON →
library5.0.4jsnpmunverified

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.

npm install vue-router
INSTALL
IMPORT
SIG · VUE-ROUTER
V
vue-router
web-frameworkjavascriptv5.0.4
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createRouter
import { createRouter } from 'vue-router'
import VueRouter from 'vue-router'
Named export for creating a router instance. Vue Router 3 (for Vue 2) used a default export or `new VueRouter()`.
createWebHistory
import { createWebHistory } from 'vue-router'
import { createHistory } from 'vue-router'
Used to configure HTML5 history mode (pushState API). Other history modes like `createWebHashHistory` or `createMemoryHistory` are also available.
RouterView
import { RouterView } from 'vue-router'
The component used to display the component matched by the current route. It's globally registered by default, but can be imported locally for explicit usage or type inference.
useRouter
import { useRouter } from 'vue-router'
Composition API hook to access the router instance within a component. For Options API, use `this.$router`.
RouteLocationRaw
import type { RouteLocationRaw } from 'vue-router'
Type import for route locations, useful for type-checking navigation calls (`router.push`) and `RouterLink` `to` prop.

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>
Debug
Known issues
breakingIn Vue Router v5.0.3, experimental features `miss()` now throws internally and returns `never`, instead of returning an error instance. Additionally, `reroute()` was added, and `NavigationResult` has been deprecated, with `selectNavigationResult` being removed.
fix
Replace `throw miss()` with just `miss()` if you were explicitly throwing it. Adapt to `reroute()` instead of `NavigationResult` and remove usages of `selectNavigationResult`.
affects: >=5.0.3
breakingFor developers migrating from `unplugin-vue-router` to Vue Router 5, import paths for the Vite plugin, data loaders, utility imports, and Volar plugins have changed.
fix
Update imports: `unplugin-vue-router/vite` to `vue-router/vite`, `unplugin-vue-router/data-loaders/*` to `vue-router/experimental`, `unplugin-vue-router` to `vue-router/unplugin`, and Volar plugins from `unplugin-vue-router/volar/*` to `vue-router/volar/*`. Remove `unplugin-vue-router` dependency.
affects: >=5.0.0
breakingThe IIFE (Immediately Invoked Function Expression) build of Vue Router 5 no longer includes `@vue/devtools-api` because it has been upgraded to v8 and does not expose an IIFE build itself.
fix
If relying on the `@vue/devtools-api` in an IIFE context, you may need to include it separately or adjust your build process. This primarily affects projects using the IIFE build directly for browser environments.
affects: >=5.0.0
breakingIn an experimental breaking change from v5.0.0-beta.1, query parameters are now optional by default. This might alter how routes with optional query parameters are matched or handled.
fix
Review routes and navigation logic that rely on the presence of query parameters. Explicitly mark them as required if their absence should lead to a different route match or behavior.
affects: >=5.0.0-beta.1
deprecatedThe `next()` callback with a string argument in navigation guards (e.g., `next('/path')`) has a deprecation warning in v5.0.3. It's recommended to use `next({ path: '/path' })` or `return '/path'` for better consistency and type safety.
fix
Update navigation guards to use `next({ path: '/path' })` or `return { path: '/path' }` for programmatic navigation, or `return '/path'` for simpler redirects. Avoid passing string arguments directly to `next()`.
affects: >=5.0.3
gotchaWhen using `createWebHistory`, server configuration is required to handle direct access to deep links (e.g., refreshing a non-root page). Without it, the server might return a 404 error.
fix
Configure your web server (e.g., Nginx, Apache, or Node.js server) to redirect all unmatched paths to your `index.html` file. This allows Vue Router to take over client-side routing.
affects: >=4.0.0
Errors
Common errors & fixes
Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location
Attempting to navigate to the same route (path and parameters) that the application is currently on, often triggered by `router.push()` or `RouterLink` clicks.
fix
Catch the promise returned by `router.push()` or `router.replace()` to handle navigation failures gracefully: `router.push(...).catch(err => { if (isNavigationFailure(err, NavigationFailureType.duplicated)) console.log('Duplicate navigation'); })`. Alternatively, ensure your navigation logic only triggers when the target route is different.
No match for current location: "/some/invalid/path"
The URL in the browser does not match any defined route in your `routes` array. This often happens with incorrect paths, typos, or missing catch-all routes.
fix
Ensure all intended paths are explicitly defined in your router's `routes` configuration. For unhandled paths, add a catch-all route (e.g., `{ path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFoundComponent }`) as the last entry in your `routes` array to render a 404 page.
Error: "[Vue Router warn]: Router must be installed explicitly using `app.use(router)`"
The Vue Router instance was created but not registered with the Vue application instance using `app.use()`.
fix
After creating your `router` instance with `createRouter()`, make sure to call `app.use(router)` on your Vue application instance before mounting it: `const app = createApp(App); app.use(router); app.mount('#app');`.
Property '$route' does not exist on type '...' or 'Property '$router' does not exist on type '...'
TypeScript error indicating that the `$route` or `$router` properties are not recognized within a component. This often occurs in `<script setup>` contexts or when types are not correctly inferred/augmented.
fix
For Composition API (`<script setup>`), use `useRoute()` and `useRouter()` hooks directly: `const route = useRoute(); const router = useRouter();`. For Options API, ensure your `tsconfig.json` correctly includes `vue-router/client` types and that global properties are properly augmented if necessary, though `this.$route` and `this.$router` usually work out of the box with `app.use(router)`.
Upgrade
Version history
5.0.4latest on npm
Audit
Dependencies
@pinia/coladaoptionalUsed for data loading features, part of the Vue ecosystem integration.
@vue/compiler-sfcrequiredNecessary for Single File Components (SFCs) compilation in a Vue 3 environment, especially with file-based routing.
piniaoptionalOften used alongside Vue Router for state management in Vue 3 applications, particularly with data loaders.
vuerequiredThe core framework that Vue Router is built for and deeply integrates with. Requires Vue 3.5.0 or higher.
Agent activity
10 hits · last 30 days
node
8
Amazon
1
OpenAI (training)
1
Resources
vue-router — npm install vue-router · libregistry