0
0
Vueframework~10 mins

Navigation guards (beforeEach, beforeEnter) in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Navigation guards (beforeEach, beforeEnter)
User tries to navigate
Global beforeEach guard runs
Route-specific beforeEnter guard runs
Navigation completes
When a user tries to change pages, Vue runs global guards first, then route-specific guards. Each guard can allow or stop navigation.
Execution Sample
Vue
router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth && !isLoggedIn()) {
    next('/login')
  } else {
    next()
  }
})
This code checks before every navigation if the target page needs login; if not logged in, it redirects to login.
Execution Table
StepGuard TypeCondition CheckedResultNavigation Action
1beforeEachto.meta.requiresAuth = true, isLoggedIn() = falseCondition trueRedirect to '/login'
2beforeEnter (for '/login')No special conditionAllowProceed to '/login'
3NavigationAt '/login'CompletePage loads '/login'
4User tries to go to '/dashboard'to.meta.requiresAuth = true, isLoggedIn() = trueCondition falseAllow navigation
5beforeEnter (for '/dashboard')No special conditionAllowProceed to '/dashboard'
6NavigationAt '/dashboard'CompletePage loads '/dashboard'
7User tries to go to '/public'to.meta.requiresAuth = falseCondition falseAllow navigation
8beforeEnter (for '/public')No special conditionAllowProceed to '/public'
9NavigationAt '/public'CompletePage loads '/public'
10User tries to go to '/admin'to.meta.requiresAuth = true, isLoggedIn() = falseCondition trueRedirect to '/login'
11Navigation abortedRedirectedStopNavigation stops, user redirected
💡 Navigation stops or proceeds based on guard checks; redirects cause new navigation cycle.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 7After Step 10Final
to.meta.requiresAuthvariestruetruefalsetruetrue or false depending on route
isLoggedIn()falsefalsetruetruefalsevaries
Navigation Actionnoneredirect to '/login'allowallowredirect to '/login'final navigation or redirect
Key Moments - 3 Insights
Why does navigation redirect to '/login' even though the user tried to go to '/admin'?
Because the global beforeEach guard checks if the route requires authentication and the user is not logged in (see execution_table step 10). It redirects before entering the route.
What happens if beforeEach calls next() without arguments?
Navigation continues to the next guard or the route itself (see execution_table steps 4 and 7 where next() allows navigation).
Can beforeEnter guard override the decision of beforeEach?
Yes, beforeEnter runs after beforeEach and can allow or block navigation independently (see execution_table steps 2 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what navigation action happens at step 1?
AAllow navigation to requested route
BAbort navigation without redirect
CRedirect to '/login'
DReload current page
💡 Hint
Check the 'Navigation Action' column at step 1 in the execution_table.
At which step does the user successfully navigate to '/dashboard'?
AStep 6
BStep 3
CStep 9
DStep 11
💡 Hint
Look for '/dashboard' in the 'Navigation' column in the execution_table.
If isLoggedIn() returned false at step 4, what would happen?
ANavigation would abort without redirect
BNavigation would redirect to '/login'
CNavigation would proceed to '/dashboard'
DNavigation would reload the current page
💡 Hint
Refer to the condition and action in execution_table step 1 and step 10 for similar cases.
Concept Snapshot
Vue Navigation Guards:
- beforeEach: global guard runs before every route change
- beforeEnter: route-specific guard runs before entering that route
- Guards check conditions and call next() to allow or redirect
- Navigation stops if next() is called with a redirect or false
- Guards run in order: beforeEach then beforeEnter
- Use guards to protect routes or redirect users
Full Transcript
When a user tries to change pages in a Vue app, the router runs global guards called beforeEach first. These guards check conditions like if the user is logged in. If the guard calls next() with a redirect path, navigation stops and restarts at the new path. If next() is called without arguments, navigation continues. After beforeEach, route-specific guards called beforeEnter run for the target route. They can also allow or block navigation. This process ensures only allowed users can access certain pages. The execution table shows steps where navigation is allowed or redirected based on login status and route requirements.