CHAPTER 15
Beginner
Dynamic Routing and Navigation Guards
Updated: May 18, 2026
5 min read
# CHAPTER 15
Dynamic Routing and Navigation Guards
1. Chapter Introduction
Real applications need dynamic routes (/user/123) and protected routes (auth required). Navigation guards are Vue Router's middleware system — they run before each navigation and can redirect, cancel, or validate route access.
2. Learning Objectives
- Create dynamic routes with params.
- Use nested (child) routes.
-
Implement
beforeEachnavigation guards.
- Protect routes with authentication guards.
- Pass data to routes via meta fields.
3. Dynamic Route Parameters
javascript
vue
4. Nested Routes
javascript
vue
5. Navigation Guards
javascript
6. Route Meta Fields
javascript
7. In-Component Guards
vue
8. Common Mistakes
-
Not watching
route.paramsfor re-renders: When navigating from/user/1to/user/2, Vue REUSES the component —onMounteddoes NOT fire again. Always watchroute.params.
-
Calling
next()multiple times: InbeforeEach, callnext()exactly once per navigation.
9. MCQs
Question 1
Dynamic route segment is defined with?
Question 2
Nested routes render in?
Question 3
beforeEach guard receives?
Question 4
next() in a guard means?
Question 5
next({ name: 'login' }) in a guard?
Question 6
Route meta fields are accessed in guard via?
Question 7
onBeforeRouteLeave fires when?
Question 8
Reusing component on param change requires?
Question 9
Optional route param syntax?
Question 10
router.afterEach is good for?
10. Interview Questions
- Q: How do you protect routes in Vue Router? Implement an auth guard.
-
Q: Why might
onMountednot fire when navigating between dynamic routes?
11. Summary
Dynamic routing with:params enables data-driven URLs. Nested routes create layout-based navigation hierarchies. Navigation guards are Vue Router's middleware — beforeEach is the primary location for authentication checks. Route meta fields carry per-route configuration.