Vue · Senior · Question 60 of 75
What is the modern way to redirect or cancel inside a Vue Router 4 guard?
router.beforeEach((to) => {
if (to.meta.requiresAuth && !auth.loggedIn) return { name: 'login', query: { redirect: to.fullPath } }
})- ✓Correct answer: Return
falseor a route location; thenext()callback is optional legacy - 2Throw an error
- 3Call
window.location.assign()
Explanation
Returning values avoids bugs like calling next() twice or never. Guards can also be async functions.