100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Vue Router Cheat Sheet

Vue Router Cheat Sheet

Covers Vue Router 4 setup, declarative and programmatic navigation, dynamic and nested routes, navigation guards, and Composition API hooks.

2 PagesIntermediateFeb 28, 2026

Basic Setup

Create and register the router (Vue 3 / Vue Router 4).

javascript
// router/index.jsimport { createRouter, createWebHistory } from 'vue-router';import Home from '../views/Home.vue';import About from '../views/About.vue';const routes = [  { path: '/', name: 'home', component: Home },  { path: '/about', name: 'about', component: About },  // Lazy-loaded route, code-split into its own chunk  { path: '/settings', component: () => import('../views/Settings.vue') },];const router = createRouter({  history: createWebHistory(),  routes,});export default router;// main.jsimport { createApp } from 'vue';import App from './App.vue';import router from './router';createApp(App).use(router).mount('#app');

Navigation

Declarative links and programmatic navigation.

javascript
<!-- Declarative --><router-link to="/about">About</router-link><router-link :to="{ name: 'user', params: { id: 123 } }">User</router-link><router-view />// Programmatic (Composition API)import { useRouter } from 'vue-router';const router = useRouter();router.push('/about');router.push({ name: 'user', params: { id: 123 } });router.replace('/login'); // no new history entryrouter.back();router.go(-1);

Dynamic & Nested Routes

Route params and layouts with child routes.

javascript
const routes = [  {    path: '/user/:id',    component: User,    props: true, // pass route.params as component props  },  {    path: '/users',    component: UsersLayout,    children: [      { path: '', component: UserList },      // /users      { path: ':id', component: UserDetail }, // /users/123    ],  },];

Navigation Guards

Control access before a route resolves.

javascript
// Global guardrouter.beforeEach((to, from) => {  if (to.meta.requiresAuth && !isAuthenticated()) {    return { name: 'login', query: { redirect: to.fullPath } };  }});// Per-route guardconst routes = [  {    path: '/admin',    component: Admin,    meta: { requiresAuth: true },    beforeEnter: (to, from) => {      console.log('Entering admin area');    },  },];

Composition API Hooks

Router access from inside setup().

  • useRouter()- Returns the router instance for programmatic navigation (push, replace, back)
  • useRoute()- Returns the current reactive route object (params, query, path, meta)
  • onBeforeRouteLeave()- Guard called when the current component's route is about to be left
  • onBeforeRouteUpdate()- Guard called when the route changes but the same component instance is reused
  • router.isReady()- Returns a Promise that resolves once the router finishes its initial navigation
Pro Tip

Prefer route.meta over hardcoding auth checks in every component — attach meta: { requiresAuth: true } to routes and enforce it once in a global router.beforeEach guard, so new protected routes are secure by default.

Was this cheat sheet helpful?

Explore Topics

#VueRouter#VueRouterCheatSheet#WebDevelopment#Intermediate#BasicSetup#Navigation#DynamicNestedRoutes#NavigationGuards#APIs#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet