Vue Router Cheatsheet

Insert Lead paragraph here.

Vue Router Cheatsheet

A quick reference for common Vue Router (v3/v4) tasks. For more details, refer to the Vue Router documentation (Vue 2) or Vue Router v4 docs (Vue 3).


1. Basic Setup

// Vue 2
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from './views/Home.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', component: Home },
]

const router = new VueRouter({
  routes // short for `routes: routes`
})

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

// Vue 3
import { createRouter, createWebHistory } from 'vue-router'
import Home from './views/Home.vue'

const routes = [
  { path: '/', component: Home },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

const app = createApp(App)
app.use(router)
app.mount('#app')

2. Route Parameters

// Route with param
{ path: '/user/:id', component: User }

// Access in component
this.$route.params.id // Vue 2
import { useRoute } from 'vue-router'
const route = useRoute()
route.params.id // Vue 3

3. Navigation

// Programmatic navigation
this.$router.push('/about') // Vue 2
import { useRouter } from 'vue-router'
const router = useRouter()
router.push('/about') // Vue 3

// With params
this.$router.push({ name: 'user', params: { id: 123 } })

4. Named Routes

{ path: '/user/:id', name: 'user', component: User }

// Navigate by name
this.$router.push({ name: 'user', params: { id: 123 } })

5. Nested Routes

const routes = [
  {
    path: '/user/:id',
    component: User,
    children: [
      { path: 'profile', component: UserProfile },
      { path: 'posts', component: UserPosts }
    ]
  }
]

6. Navigation Guards

// Global
router.beforeEach((to, from, next) => { /* ... */ })

// Per-route
{
  path: '/admin',
  component: Admin,
  beforeEnter: (to, from, next) => { /* ... */ }
}

// In-component
export default {
  beforeRouteEnter(to, from, next) { /* ... */ },
  beforeRouteUpdate(to, from, next) { /* ... */ },
  beforeRouteLeave(to, from, next) { /* ... */ },
}

7. Redirects & Aliases

{ path: '/home', redirect: '/' }
{ path: '/about', alias: '/info' }

8. Lazy Loading

const Foo = () => import('./Foo.vue')

9. Query Parameters

// /search?q=vue
this.$route.query.q // Vue 2
route.query.q // Vue 3 (with useRoute)

<router-link to="/about">About</router-link>
<router-link :to="{ name: 'user', params: { id: 123 } }">User</router-link>

11. Active Classes

<router-link to="/" active-class="my-active-class" exact-active-class="my-exact-active-class">Home</router-link>

12. History Modes

// Vue 2
new VueRouter({
  mode: 'history', // or 'hash'
  routes
})

// Vue 3
createRouter({
  history: createWebHistory(), // or createWebHashHistory()
  routes
})

13. Scroll Behavior

const router = new VueRouter({
  routes,
  scrollBehavior (to, from, savedPosition) {
    return savedPosition || { x: 0, y: 0 }
  }
})

14. 404 Not Found Route

{ path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound } // Vue 3
{ path: '*', component: NotFound } // Vue 2

15. Route Meta Fields

{ path: '/admin', component: Admin, meta: { requiresAuth: true } }

// Use in navigation guard
if (to.meta.requiresAuth) { /* ... */ }

References: