vuejs / router

🚦 The official router for Vue.js
https://router.vuejs.org/
MIT License
3.95k stars 1.19k forks source link

return and next behave differently in the beforeEach guard. #2334

Closed 1264362700 closed 3 months ago

1264362700 commented 3 months ago

Reproduction

production

Steps to reproduce the bug

image image

Expected behavior

I'm encountering an unexpected behavior with Vue Router's beforeEach guard. Despite following the documented approach of returning to.fullPath to initiate a redirect, the navigation isn't occurring as anticipated. I've discovered that employing next within the guard is the sole method to successfully trigger a redirect.

Actual behavior

image

I'm encountering an inconsistency in Vue Router's beforeEach guard behavior. While utilizing the next method results in a successful second route match, returning to.fullPath doesn't trigger a subsequent guard execution. This discrepancy leads to an unrendered page.

Additional information

I'm encountering an issue with Vue Router's beforeEach guard. I'm expecting return to.fullPath to initiate a redirect, but the guard isn't being re-evaluated and the route doesn't match. Switching to next(to) resolves the issue. Could you please explain the underlying mechanism and why this behavior occurs? vue-router version: 4.0.16 vue version: 3.2.45

1264362700 commented 3 months ago

image I noticed the documentation recommends using return to.fullPath to trigger redirects, but this method doesn't seem to re-trigger the route guard. Is this unexpected behavior or is it by design?