Vue Router Next

Posted on  by admin
Modified2 years, 9 months ago.

I have the following code:. I'm trying to get the current user, and if this succeeds, then do something with this data, and if the request is not successful, then redirect the user to the login page.

But next () calls do not work, I get the "then" or "catch" in the console, but the redirect does not occur and an infinite loop begins. But if I take next () from condition (commented row) the redirect works fine.

Gosha nullGosha null. To redirect you should use next('/') or next({ path: '/' }). From the documentation:. next: Function: this function must be called to resolve the hook. The action depends on the arguments provided to next:.

next(): move on to the next hook in the pipeline. If no hooks are left, the navigation is confirmed. next(false): abort the current navigation. If the browser URL was changed (either manually by the user or via back button), it will be reset to that of the from route.

next('/') or next({ path: '/' }): redirect to a different location.

The current navigation will be aborted and a new one will be started.

You can pass any location object to next, which allows you to specify options like replace: true, name: 'home' and any option used in router-link's to prop or router.push.
259259 bronze badges.
1

The promise resolves after the function ends. This means that the commented next happens regardless of the result of the promise result.

Then the promise resolves and you call another next.

Quickstart