Top Related Projects
Declarative routing for React
🥢 A minimalist-friendly ~2.1KB routing for React and Preact
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 nested route/view mapping, modular, component-based router configuration, route params, query, wildcards, and more.
Pros
- Seamless integration with Vue.js ecosystem
- Supports nested routes and views
- Provides programmatic navigation
- Offers fine-grained navigation control with navigation guards
Cons
- Learning curve for beginners
- Can be overkill for simple applications
- Limited built-in support for code-splitting (requires manual configuration)
- 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:
import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import App from './App.vue'
import Home from './components/Home.vue'
import About from './components/About.vue'
const router = createRouter({
history: createWebHistory(),
routes: [
{ path: '/', component: Home },
{ path: '/about', component: About },
]
})
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 mature and battle-tested, with a larger ecosystem of extensions and integrations
- Supports both web and React Native applications
- Offers more advanced features like nested routes and route-based code splitting
Cons of React Router
- Can be more complex to set up and configure, especially for beginners
- Requires more boilerplate code for basic routing scenarios
- Less tightly integrated with the React ecosystem compared to Vue Router with Vue.js
Code Comparison
React Router:
import { BrowserRouter, Route, Switch } from 'react-router-dom';
<BrowserRouter>
<Switch>
<Route path="/about" component={About} />
<Route path="/" exact component={Home} />
</Switch>
</BrowserRouter>
Vue Router:
import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
history: createWebHistory(),
routes: [
{ path: '/about', component: About },
{ path: '/', component: Home }
]
})
Both React Router and Vue Router are popular routing solutions for their respective frameworks. React Router offers more flexibility and advanced features, while Vue Router provides a simpler and more integrated experience for Vue.js applications. The choice between them often depends on the specific project requirements and the developer's familiarity with each ecosystem.
🥢 A minimalist-friendly ~2.1KB routing for React and Preact
Pros of wouter
- Lightweight and minimalistic (1.5kB gzipped) compared to Vue Router
- Framework-agnostic, works with React, Preact, and other libraries
- Simple API with hooks-based approach for React
Cons of wouter
- Less feature-rich than Vue Router (e.g., no built-in navigation guards)
- Smaller community and ecosystem
- Limited documentation compared to Vue Router's extensive guides
Code Comparison
Vue Router:
const router = new VueRouter({
routes: [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar }
]
})
wouter:
import { Route, Switch } from "wouter";
<Switch>
<Route path="/foo" component={Foo} />
<Route path="/bar" component={Bar} />
</Switch>
Both 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 and extensive documentation. wouter, on the other hand, is a lightweight, flexible alternative that can be used across different frameworks, making it suitable for smaller projects or when minimal routing functionality is required.
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 the repository for Vue Router 4 (for Vue 3)
- For Vue Router 3 (for Vue 2) see vuejs/vue-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:
Silver Sponsors
Bronze Sponsors
Get started with the documentation.
Quickstart
-
Via CDN:
<script src="https://unpkg.com/vue-router@4"></script>
-
In-browser playground on CodeSandbox
-
Add it to an existing Vue Project:
npm install vue-router@4
Changes from Vue Router 3
Please consult the Migration Guide.
Contributing
See Contributing Guide.
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
🥢 A minimalist-friendly ~2.1KB routing for React and Preact
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