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?
router.beforeEach((to, from, next) => { if (!isAuthenticated() && to.name !== 'login') { next({ name: 'login' }); } else { next(); } })
Think about when next() is called with a redirect object.
The beforeEach guard intercepts navigation before the route changes. If the user is not authenticated, calling next({ name: 'login' }) immediately redirects to the login page without rendering the protected page.
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?
let accessGranted = false; const routes = [ { path: '/dashboard', beforeEnter: (to, from, next) => { if (to.query.token === 'abc123') { accessGranted = true; next(); } else { next(false); } } } ];
Check the condition inside the beforeEnter guard and the query parameter value.
The beforeEnter guard sets accessGranted to true only if the query parameter token equals 'abc123'. Since the URL has ?token=abc123, the condition is true and accessGranted becomes true.
Identify which beforeEach guard code snippet will cause a syntax error in Vue Router.
Look carefully at the syntax of the if statement.
Option C is missing parentheses around the if condition, which is required in JavaScript syntax. This causes a syntax error.
In a beforeEach guard, the code calls next() twice under certain conditions. What is the effect on navigation?
router.beforeEach((to, from, next) => { if (to.name === 'home') { next(); next(false); } else { next(); } })
Check what happens if next() is called more than once.
Calling next() multiple times in the same guard causes Vue Router to throw an error and can crash the app or break navigation.
Vue Router supports multiple navigation guards: beforeEach, beforeEnter, and component guards like beforeRouteEnter. Which guard runs first when navigating to a new route?
Think about the order of global vs route-specific guards.
The global beforeEach guard runs first before any route-specific beforeEnter or component guards. This allows global checks before entering any route.