Vue Routing
🚦 Vue Routing (Vue Router)
Routing in Vue.js is handled using Vue Router.
1️⃣ What is Routing in Vue? ⭐
Routing means:
-
Mapping URL paths to Vue components
-
Changing views without page refresh
-
Essential for SPA development
📌 Example:
2️⃣ What is Vue Router? ⭐⭐
-
Official routing library for It
-
Manages URLs & components
-
Supports:
-
Dynamic routes
-
Nested routes
-
Route guards
-
Lazy loading
-
3️⃣ Installing Vue Router ⭐
Vue 3
4️⃣ Basic Router Setup ⭐⭐
📁 router/index.js
5️⃣ Register Router in App ⭐
main.js
6️⃣ <router-view> and <router-link> ⭐⭐⭐
Router View
📌 Displays matched component
Router Link
📌 SPA navigation (no reload)
7️⃣ Dynamic Routing ⭐⭐
Access Parameter
📌 URL: /user/10
8️⃣ Nested Routes ⭐⭐⭐
📌 URL:
-
/dashboard/profile -
/dashboard/settings
9️⃣ Programmatic Navigation ⭐⭐
With Params
🔟 Route Guards (Security) ⭐⭐⭐
Global Guard
📌 Used for:
-
Authentication
-
Authorization
1️⃣1️⃣ Lazy Loading Routes ⭐⭐
📌 Improves performance
1️⃣2️⃣ History Modes ⭐⭐
| Mode | Description |
|---|---|
createWebHistory() |
Clean URLs (/about) |
createWebHashHistory() |
Hash URLs (#/about) |
📌 Interview Questions
Q1. What is Vue Router?
👉 Official routing library for Vue
Q2. What is <router-view>?
👉 Renders matched component
Q3. Difference between router-link & <a>?
👉 router-link avoids reload
Q4. 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
