You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using `router-link`, Vue Router internally calls `router.push` to trigger a navigation. Depending on the current location and existing [Navigation Guards](./navigation-guards.md), this navigation might end up in a new page being shown, but there are a couple of situations where we will stay on the same page:
4
+
5
+
- We are already on the page we are trying to go to
6
+
- A navigation guard aborts the navigation by calling `next(false)`
7
+
- An error is thrown in a navigation by calling `next(new Error())`
8
+
9
+
When using a regular `router-link`, **none of these failures will log an error**. However, if you are using `router.push` or `router.replace`, you might come across an _"Uncaught (in promise) Error"_ message followed by a more specific message in your console. This is part of the _Navigation Failures_ system in Vue Router and it is only available from version 3.2.0 onwards but existed for a long time before: through the two optional callbacks, `onComplete` and `onAbort` that can be passed to `router.push`. Since version 3.1.0, `router.push` and `router.replace` return a _Promise_ if no `onComplete`/`onAbort` callback is provided. This _Promise_ resolves instead of invoking `osComplete` and rejects instead of invoking `onAbort`.
10
+
11
+
## Detecting Navigation Failures
12
+
13
+
_Navigation Failures_ are `Error` instances with a few extra properties. Among them, you can find a `type` property. This will allow you to check the type of the navigation failure:
if (failure.type===NavigationFailureTypes.redirected) {
22
+
// show a small notification to the user
23
+
showToast('Login in order to access the admin panel')
24
+
}
25
+
}
26
+
})
27
+
```
28
+
29
+
## `NavigationFailureTypes`
30
+
31
+
`NavigationFailureTypes` exposes the following properties to differentiate _Navigation Failures_:
32
+
33
+
-`redirected`: `next(newLocation)` was called inside of a navigation guard to redirect somewhere else.
34
+
-`aborted`: `next(false)` was called inside of a navigation guard to the navigation.
35
+
-`cancelled`: A new navigation completely took place before the current navigation could finish. e.g. `router.push` was called while waiting inside of a navigation guard.
36
+
-`duplicated`: The navigation was prevented because we are already at the target location.
37
+
38
+
## _Navigation Failures_'s properties
39
+
40
+
Apart from exposing a `type` property, all navigation failures expose `to` and `from` properties to reflect the current location as well as the target location for the navigation that failed:
41
+
42
+
```js
43
+
// given we are at `/`
44
+
router.push('/admin').catch(failure=> {
45
+
if (failure) {
46
+
if (failure.type===NavigationFailureTypes.redirected) {
47
+
failure.to.path// '/admin'
48
+
failure.from.path// '/'
49
+
}
50
+
}
51
+
```
52
+
53
+
In all cases, `from` and `to` are normalized route locations.
0 commit comments