Navigation guards help control access to pages in a Vue app. They let you check or stop navigation before it happens.
To check if a user is logged in before entering a page.
To show a warning before leaving a form with unsaved changes.
To redirect users from old URLs to new ones.
To load data before showing a page.
To block access to admin pages for regular users.
Syntax
Vue
router.beforeEach((to, from, next) => {
// code to run before every route change
next()
})
const route = {
path: '/secret',
component: SecretPage,
beforeEnter: (to, from, next) => {
// code to run before entering this route
next()
}
}
beforeEach runs before every route change in the app.
beforeEnter runs only before entering a specific route.
Examples
Redirects to login if the page needs login and user is not logged in.
This Vue router setup uses beforeEach to log every navigation. The beforeEnter guard on the dashboard route checks if the user is logged in. If not, it redirects to login.
Always call next() to continue navigation or redirect.
Use beforeEach for global checks and beforeEnter for route-specific checks.
Navigation guards help improve app security and user experience.
Summary
Navigation guards run code before changing pages.
beforeEach runs for every route change.
beforeEnter runs only for a specific route.
Practice
(1/5)
1. What is the main purpose of Vue Router's beforeEach navigation guard?
easy
A. To run a function before every route change globally
B. To run a function only after a route has been entered
C. To modify the route's component before rendering
D. To delay the route change indefinitely
Solution
Step 1: Understand the role of beforeEach
beforeEach is a global navigation guard that runs before every route change in the app.
Step 2: Compare options with the guard's behavior
To run a function before every route change globally correctly states it runs before every route change globally. Other options describe incorrect or unrelated behaviors.
Final Answer:
To run a function before every route change globally -> Option A
Quick Check:
Global guard before route change = A [OK]
Hint: beforeEach runs before every route change globally [OK]
Common Mistakes:
Confusing beforeEach with afterEach
Thinking beforeEach runs only on specific routes
Assuming beforeEach modifies components directly
2. Which of the following is the correct syntax to add a beforeEnter guard to a route in Vue Router?
B. Console logs 'Global guard' and navigates to /dashboard regardless
C. Console logs 'Global guard' and navigates to /login if not logged in
D. Syntax error due to missing next() call
Solution
Step 1: Analyze global beforeEach guard
The global guard logs 'Global guard' and calls next(), so navigation proceeds.
Step 2: Analyze route-specific beforeEnter guard
beforeEnter checks if user is logged in. If not, it redirects to '/login'. Otherwise, it allows navigation.
Final Answer:
Console logs 'Global guard' and navigates to /login if not logged in -> Option C
Quick Check:
Global guard logs + route guard redirects if not logged in = A [OK]
Hint: Global guard runs first, then route guard decides next step [OK]
Common Mistakes:
Ignoring global guard console log
Assuming navigation always succeeds
Forgetting next() call in guards
4. Identify the error in this Vue Router navigation guard code:
router.beforeEach((to, from, next) => {
if (to.meta.requiresAuth) {
if (!userIsLoggedIn) {
next('/login')
}
}
next()
})
medium
A. Missing call to next() inside the if block causes navigation to continue twice
B. userIsLoggedIn should be a function call
C. to.meta.requiresAuth is not a valid property
D. next() is called twice, causing navigation to fail
Solution
Step 1: Trace next() calls in the guard
If to.meta.requiresAuth is true and user is not logged in, next('/login') is called. Then outside the if block, next() is called again unconditionally.
Step 2: Understand consequences of calling next() twice
Calling next() twice causes Vue Router to throw an error and navigation fails.
Final Answer:
next() is called twice, causing navigation to fail -> Option D
Quick Check:
Two next() calls in one guard = B [OK]
Hint: Call next() only once per guard to avoid errors [OK]
Common Mistakes:
Calling next() multiple times in one guard
Not returning after next('/login')
Assuming meta is always defined
5. You want to protect a nested route /admin/settings so that only logged-in users can access it. You have a global beforeEach guard and a route-level beforeEnter guard on /admin. Which approach correctly ensures the user is redirected to /login if not logged in, without redundant checks?
hard
A. Check login only in global beforeEach; remove beforeEnter guard from /admin route
B. Check login only in beforeEnter guard on /admin; global beforeEach does not check login
C. Check login in both global beforeEach and beforeEnter guard on /admin
D. Do not check login in any guard; rely on component logic
Solution
Step 1: Understand guard execution order and redundancy
Global beforeEach runs before every route, including nested ones. Route-level beforeEnter runs after global guards.
Step 2: Avoid redundant login checks
Checking login in both guards causes duplicate redirects and complexity. Checking only in global guard covers all routes.
Final Answer:
Check login only in global beforeEach; remove beforeEnter guard from /admin route -> Option A
Quick Check:
Single global guard for login check avoids redundancy = A [OK]
Hint: Use global guard for login checks to avoid duplicate logic [OK]
Common Mistakes:
Duplicating login checks in multiple guards
Relying on component logic instead of guards
Ignoring nested route guard order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.