Vue Routing

Vue Tutorial

Vue Routing (Vue Router)

Vue Routing allows you to build Single Page Applications (SPA) where navigation happens without reloading the page.

Routing in It is handled using Vue Router.


What is Routing in It?

Routing means:

  • Mapping URL paths to Vue components

  • Changing views without page refresh

  • Essential for SPA development

 Example:

/home → Home.vue
/about → About.vue

 What is Vue Router?

  • Official routing library for It

  • Manages URLs & components

  • Supports:

    • Dynamic routes

    • Nested routes

    • Route guards

    • Lazy loading


 Installing Vue Router

Vue 3

npm install vue-router

 Basic Router Setup

router/index.js


 


Register Router in App

main.js


 


<router-view> and <router-link>

Router View

<router-view />
  • Displays matched component

Router Link

  •  SPA navigation (no reload)

Dynamic Routing

Access Parameter

 URL: /user/10


Nested Routes

 URL:

  • /dashboard/profile

  • /dashboard/settings


Programmatic Navigation

With Params


Route Guards (Security)

Global Guard

Used for:

  • Authentication

  • Authorization


Lazy Loading Routes

  •  Improves performance

History Modes

ModeDescription
createWebHistory()Clean URLs (/about)
createWebHashHistory()Hash URLs (#/about)

Interview Questions

Q1. What is <router-view>?
 Renders matched component

Q2. Difference between router-link & <a>?
router-link avoids reload

Q3. What are route guards?
 Control navigation access


 Summary

  •  Vue Router enables SPA navigation
  • Routes map URL → components
  • <router-view> renders components
  •  Supports dynamic & nested routes
  •  Route guards handle security
  •  Lazy loading improves speed

You may also like...