Chapter 37: Vue Routing

Vue Routing (Vue Router), one of the most important parts of building any real Vue application that has more than one page.

In February 2026, almost every Vue 3 project that is more than a single-page prototype uses Vue Router (official routing library). Without routing, your app is just one giant component — with routing, it becomes a proper multi-page Single Page Application (SPA) with clean URLs, back/forward button support, deep linking, lazy loading, authentication guards, nested views, etc.

Let’s go through it step by step like a proper classroom session — no rushing, lots of real examples.

1. What is Vue Routing? (Simple Human Explanation)

Imagine your app is a house with many rooms:

  • Home page (landing / dashboard)
  • Profile page
  • Settings page
  • Products list
  • Product detail (dynamic URL)
  • Login page
  • 404 Not Found page

Vue Router is the smart doorman + map of the house:

  • When user types /profile or clicks a link → router opens the correct room (component)
  • When user clicks browser back button → router takes them to the previous room
  • When you want to go programmatically (after login) → router says router.push(‘/dashboard’)
  • When you want a URL that changes without full page reload → that’s SPA magic (no <a href> reloads)

Vue Router handles all this declaratively — you define routes (path → component mapping), and Vue takes care of the rest.

2. Current Standard in 2026: Vue Router 4 + Vite

Vue Router 4 is the version used with Vue 3. Installation (if you didn’t choose router during npm create vite):

Bash

3. Step-by-Step Setup (Modern 2026 Way)

1. Create router file src/router/index.ts

TypeScript

2. Connect router to app src/main.ts

TypeScript

3. Make App.vue just a wrapper with <router-view>

vue

4. Navigation – How Users Move Between Pages

A. Using <router-link> (preferred – declarative)

vue

B. Programmatic navigation (in JavaScript)

vue

5. Dynamic Routes (Params)

TypeScript

In ProfileView.vue

vue

6. Nested Routes (Very Common in Real Apps)

TypeScript

DashboardLayout.vue (parent with <router-view>)

vue

Quick Summary Table – Vue Router Essentials (2026)

Feature Syntax / Pattern Why / When to use it
Basic route { path: ‘/about’, component: AboutView } Static pages
Named route name: ‘profile’ → router.push({ name: ‘profile’ }) Refactor-safe links
Dynamic param path: ‘/user/:id’ → :to=”{ params: { id: 123 } }” Detail pages
Nested routes children: [ … ] + <router-view> in parent Layouts with sidebar
Lazy loading component: () => import(‘@/views/Profile.vue’) Faster initial load
Navigation guard router.beforeEach((to, from) => { … }) Auth, analytics
Query params ?search=vue → to.query.search Filters, pagination
404 page { path: ‘/:pathMatch(.*)*’, component: NotFound } Catch-all

Your First Mini Project After This Lesson

  1. Create HomeView.vue, AboutView.vue, ProfileView.vue
  2. Set up router with 3 routes + 404
  3. Add <router-link> navigation bar in App.vue
  4. Add a simple auth guard (fake login button → redirect to /profile)

Any part confusing? Want to see:

  • Full auth flow with login redirect & protected routes?
  • Nested routes with sidebar layout?
  • Route meta + navigation guards with Pinia?
  • Query params + pagination example?
  • Programmatic navigation after form submit?

Just tell me — we’ll build the next real routing example together 🚀

Happy routing from Hyderabad! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *