Top Related Projects
Declarative routing for React
🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering.
🥢 A minimalist-friendly ~2.1KB routing for React and Preact
A simple vanilla JavaScript router.
Quick Overview
Vue Router is the official routing library for Vue.js applications. It deeply integrates with Vue.js core to make building Single Page Applications with Vue.js a breeze. Vue Router provides powerful features for mapping components to routes, nested route/view mapping, and fine-grained navigation control.
Pros
- Seamless integration with Vue.js ecosystem
- Supports nested routes and views
- Offers programmatic navigation and dynamic route matching
- Provides route-level code splitting for better performance
Cons
- Learning curve for developers new to Vue.js ecosystem
- Limited built-in support for server-side rendering (SSR)
- Can be overkill for simple applications with minimal routing needs
- Occasional breaking changes between major versions
Code Examples
- Basic route configuration:
import { createRouter, createWebHistory } from 'vue-router'
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About },
{ path: '/user/:id', component: User }
]
const router = createRouter({
history: createWebHistory(),
routes
})
- Navigation guard example:
router.beforeEach((to, from) => {
if (to.meta.requiresAuth && !isAuthenticated()) {
return { name: 'Login' }
}
})
- Programmatic navigation:
// Navigate to a specific route
router.push('/user/123')
// Navigate with named route
router.push({ name: 'user', params: { id: '123' } })
// Go back in history
router.go(-1)
Getting Started
- Install Vue Router:
npm install vue-router@4
- Create a router instance in your main.js file:
import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import App from './App.vue'
const router = createRouter({
history: createWebHistory(),
routes: [
{ path: '/', component: () => import('./components/Home.vue') },
{ path: '/about', component: () => import('./components/About.vue') }
]
})
const app = createApp(App)
app.use(router)
app.mount('#app')
- Use router-view component in your App.vue:
<template>
<router-view></router-view>
</template>
Competitor Comparisons
Declarative routing for React
Pros of React Router
- More flexible and powerful, supporting advanced routing patterns
- Better integration with React's component model
- Larger ecosystem and community support
Cons of React Router
- Steeper learning curve, especially for complex scenarios
- More verbose syntax for basic routing tasks
- Less opinionated, which can lead to inconsistent implementations
Code Comparison
Vue Router:
const router = new VueRouter({
routes: [
{ path: '/user/:id', component: User }
]
})
React Router:
<BrowserRouter>
<Routes>
<Route path="/user/:id" element={<User />} />
</Routes>
</BrowserRouter>
Both Vue Router and React Router are popular routing solutions for their respective frameworks. Vue Router is designed specifically for Vue.js applications, offering a simpler API and tighter integration with Vue's reactivity system. It's generally easier to get started with and provides a more streamlined experience for basic routing needs.
React Router, on the other hand, offers more flexibility and power, making it suitable for complex routing scenarios. It aligns well with React's component-based architecture and provides features like nested routes and route-based code splitting out of the box.
The choice between the two often comes down to the framework being used (Vue.js or React) and the specific routing requirements of the project.
🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering.
Pros of TanStack Router
- Framework-agnostic: Works with React, Vue, Solid, and other frameworks
- Type-safe routing with automatic type inference
- Built-in search params handling and nested routing
Cons of TanStack Router
- Smaller community and ecosystem compared to Vue Router
- Steeper learning curve for developers familiar with traditional routing libraries
- Less integration with Vue-specific features and conventions
Code Comparison
Vue Router:
const router = createRouter({
history: createWebHistory(),
routes: [
{ path: '/', component: Home },
{ path: '/about', component: About },
]
})
TanStack Router:
const routeTree = rootRoute({
component: Root,
children: [
{ path: '/', component: Home },
{ path: 'about', component: About },
],
})
Both Vue Router and TanStack Router are powerful routing solutions, but they cater to different needs. Vue Router is deeply integrated with the Vue ecosystem, offering a seamless experience for Vue developers. It provides a familiar API and extensive documentation.
TanStack Router, on the other hand, offers a more flexible approach that can be used across multiple frameworks. It emphasizes type safety and modern routing patterns, making it an attractive option for developers working on complex applications or those who value strong typing.
The choice between the two depends on the specific project requirements, the development team's preferences, and the overall tech stack being used.
🥢 A minimalist-friendly ~2.1KB routing for React and Preact
Pros of wouter
- Lightweight and minimalistic (only ~1KB gzipped)
- Framework-agnostic, works with React, Preact, and other libraries
- Simple API with hooks-based approach
Cons of wouter
- Less feature-rich compared to vue-router
- Smaller community and ecosystem
- May require additional setup for more complex routing scenarios
Code Comparison
vue-router:
const router = new VueRouter({
routes: [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar }
]
})
wouter:
import { Route, Switch } from "wouter";
function App() {
return (
<Switch>
<Route path="/foo" component={Foo} />
<Route path="/bar" component={Bar} />
</Switch>
);
}
Both vue-router and wouter provide routing solutions for web applications, but they cater to different needs. vue-router is a more comprehensive solution specifically designed for Vue.js applications, offering advanced features like nested routes, navigation guards, and lazy loading. On the other hand, wouter is a lightweight, framework-agnostic alternative that focuses on simplicity and flexibility. It's particularly well-suited for smaller projects or those using React-like libraries. The choice between the two depends on the specific requirements of your project and the framework you're using.
A simple vanilla JavaScript router.
Pros of Navigo
- Lightweight and framework-agnostic, making it suitable for various JavaScript projects
- Simple API with straightforward setup and usage
- Supports hash-based and history API routing
Cons of Navigo
- Less feature-rich compared to Vue Router
- Smaller community and ecosystem
- Limited integration with Vue.js specific features
Code Comparison
Vue Router:
const router = new VueRouter({
routes: [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar }
]
})
Navigo:
const router = new Navigo('/');
router
.on('/foo', () => { /* render Foo component */ })
.on('/bar', () => { /* render Bar component */ });
Vue Router is specifically designed for Vue.js applications, offering seamless integration with Vue components and features like navigation guards and lazy loading. It provides a more robust solution for complex Vue.js applications with advanced routing needs.
Navigo, on the other hand, is a lightweight and flexible routing solution that can be used in various JavaScript projects, including Vue.js applications. It offers a simpler API and is easier to set up for basic routing needs. However, it lacks some of the advanced features and Vue-specific integrations that Vue Router provides.
Choose Vue Router for Vue.js applications with complex routing requirements, and consider Navigo for simpler projects or when working with other JavaScript frameworks.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
vue-router
This is vue-router 3.0 which works only with Vue 2.0.
- For the 1.x router see the 1.0 branch.
- For Vue Router 4 (for Vue 3) see vuejs/router.
Supporting Vue Router
Vue Router is part of the Vue Ecosystem and is an MIT-licensed open source project with its ongoing development made possible entirely by the support of Sponsors. If you would like to become a sponsor, please consider:
Gold Sponsors
Silver Sponsors
Bronze Sponsors
Get started with the documentation, or play with the examples (see how to run them below).
Development Setup
# install deps
yarn
# build dist files
yarn build
# serve examples at localhost:8080
yarn dev
# lint & run all tests
yarn test
# serve docs at localhost:8080
yarn docs
Releasing
yarn run release
- Ensure tests are passing
yarn run test
- Build dist files
VERSION=<the_version> yarn run build
- Build changelog
yarn run changelog
- Commit dist files
git add dist CHANGELOG.md && git commit -m "[build $VERSION]"
- Publish a new version `npm version $VERSION --message "[release] $VERSION"
- Push tags
git push origin refs/tags/v$VERSION && git push
- Publish to npm
npm publish
- Ensure tests are passing
Questions
For questions and support please use the Discord chat server or the official forum. The issue list of this repo is exclusively for bug reports and feature requests.
Issues
Please make sure to read the Issue Reporting Checklist before opening an issue. Issues not conforming to the guidelines may be closed immediately.
Contribution
Please make sure to read the Contributing Guide before making a pull request.
Changelog
Details changes for each release are documented in the CHANGELOG.md file
.
Stay In Touch
- For latest releases and announcements, follow on Twitter: @vuejs
License
Copyright (c) 2013-present Evan You
Special Thanks
Special thanks to BrowserStack for letting the maintainers use their service to debug browser specific issues.
Top Related Projects
Declarative routing for React
🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering.
🥢 A minimalist-friendly ~2.1KB routing for React and Preact
A simple vanilla JavaScript router.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot