Complete the code to add a global navigation guard that runs before each route change.
router.[1]((to, from, next) => { console.log('Navigation started'); next(); });
The beforeEach method adds a global guard that runs before every route change.
Complete the code to add a route-specific guard that runs before entering the '/dashboard' route.
const routes = [
{
path: '/dashboard',
component: Dashboard,
[1]: (to, from, next) => {
if (isLoggedIn()) next();
else next('/login');
}
}
];The beforeEnter guard runs only before entering this specific route.
Fix the error in the global guard code to correctly call the next function.
router.beforeEach((to, from, [1]) => { if (to.meta.requiresAuth && !isLoggedIn()) { next('/login'); } else { next(); } });
The next function must be called to resolve the navigation guard.
Fill both blanks to create a global guard that blocks navigation to '/admin' if the user is not an admin.
router.[1]((to, from, next) => { if (to.path === '/admin' && !userIsAdmin()) { next({ path: [2] }); } else { next(); } });
Use beforeEach for global guard and redirect to /login if not admin.
Fill all three blanks to create a route with a beforeEnter guard that checks if the user is logged in and redirects to '/login' if not.
const routes = [
{
path: '/profile',
component: Profile,
[1]: (to, from, next) => {
if (![2]()) {
next({ path: [3] });
} else {
next();
}
}
}
];The beforeEnter guard checks login status with isLoggedIn() and redirects to '/login' if not logged in.