0
0
Vueframework~20 mins

Navigation guards (beforeEach, beforeEnter) in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Navigation Guard Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a global beforeEach guard blocks navigation?

Consider a Vue Router setup with a beforeEach guard that checks if a user is authenticated. If not, it redirects to a login page.

What will be the rendered output if a user tries to visit a protected route without being authenticated?

Vue
router.beforeEach((to, from, next) => {
  if (!isAuthenticated() && to.name !== 'login') {
    next({ name: 'login' });
  } else {
    next();
  }
})
AThe user is immediately redirected to the login page without seeing the protected page.
BThe user sees the protected page content briefly, then is redirected to login.
CThe navigation is blocked and the user stays on the current page with no change.
DThe router throws an error because next() is called twice.
Attempts:
2 left
💡 Hint

Think about when next() is called with a redirect object.

state_output
intermediate
1:30remaining
What is the value of 'accessGranted' after beforeEnter guard runs?

Given a route with a beforeEnter guard that sets a variable accessGranted to true only if a query parameter token equals 'abc123'. What is the value of accessGranted after navigating to /dashboard?token=abc123?

Vue
let accessGranted = false;
const routes = [
  {
    path: '/dashboard',
    beforeEnter: (to, from, next) => {
      if (to.query.token === 'abc123') {
        accessGranted = true;
        next();
      } else {
        next(false);
      }
    }
  }
];
Afalse
Btrue
Cnull
Dundefined
Attempts:
2 left
💡 Hint

Check the condition inside the beforeEnter guard and the query parameter value.

📝 Syntax
advanced
1:30remaining
Which option causes a syntax error in a beforeEach guard?

Identify which beforeEach guard code snippet will cause a syntax error in Vue Router.

Arouter.beforeEach((to, from, next) => { if (to.meta.requiresAuth) { next(); } else { next(false); } })
Brouter.beforeEach((to, from, next) => { if (to.meta.requiresAuth) next(); else next(false); })
Crouter.beforeEach((to, from, next) => { if to.meta.requiresAuth { next(); } else { next(false); } })
D)} ;)eslaf(txen esle ;)(txen )htuAseriuqer.atem.ot( fi { >= )txen ,morf ,ot((hcaEerofeb.retuor
Attempts:
2 left
💡 Hint

Look carefully at the syntax of the if statement.

🔧 Debug
advanced
2:00remaining
Why does navigation get stuck when using next() incorrectly?

In a beforeEach guard, the code calls next() twice under certain conditions. What is the effect on navigation?

Vue
router.beforeEach((to, from, next) => {
  if (to.name === 'home') {
    next();
    next(false);
  } else {
    next();
  }
})
ANavigation proceeds normally to the 'home' route.
BNavigation is aborted and the user stays on the current page.
CNavigation proceeds but with a warning in the console.
DNavigation causes an error and the app crashes.
Attempts:
2 left
💡 Hint

Check what happens if next() is called more than once.

🧠 Conceptual
expert
2:30remaining
Which guard type runs first during navigation in Vue Router?

Vue Router supports multiple navigation guards: beforeEach, beforeEnter, and component guards like beforeRouteEnter. Which guard runs first when navigating to a new route?

A<code>beforeEach</code> global guard
B<code>beforeEnter</code> guard on the route
CComponent's <code>beforeRouteEnter</code> guard
DAfter navigation is confirmed
Attempts:
2 left
💡 Hint

Think about the order of global vs route-specific guards.