-
-
Notifications
You must be signed in to change notification settings - Fork 5k
docs: add navigation failures #3220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e24c57f
docs: add navigation failures
posva e69d78b
Update docs/guide/advanced/navigation-failures.md
posva a554157
Update docs/guide/advanced/navigation-failures.md
posva 02f7eaa
feat(errors): NavigationDuplicated name for backwards compatibility
posva a93434f
docs: use singular form
posva cea40b2
feat(errors): expose isRouterError
posva c567ef5
refactor: fix flow types
posva c47f9c4
types(errors): add TS types
posva bb90585
docs: add new in comment [skip ci]
posva 902c82d
docs: rename isRouterError [skip ci]
posva e532401
docs: remove extra if [skip ci]
posva 5e03c90
docs: split background story and explanation [skip ci]
posva 4492bbb
Apply suggestions from code review
posva 73c1d58
refactor: adapt values for errors
posva 7ef00d1
Apply suggestions from code review
posva 7fd4b93
refactor: isRouterError -> isNavigationFailure
posva d8ce259
refactor: add missing type rename [skip ci]
posva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Navigation Failures | ||
|
||
> New in 3.4.0 | ||
|
||
When using `router-link`, Vue Router calls `router.push` to trigger a navigation. While the expected behavior for most links is to navigate a user to a new page, there are a few situations where users will remain on the same page: | ||
|
||
- Users are already on the page that they are trying to navigate to | ||
- A [navigation guard](./navigation-guards.md) aborts the navigation by calling `next(false)` | ||
- A [navigation guard](./navigation-guards.md) throws an error or calls `next(new Error())` | ||
|
||
When using a `router-link` component, **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. Let's understand how to differentiate _Navigation Failures_. | ||
|
||
::: tip Background story | ||
In v3.2.0, _Navigation Failures_ were exposed through the two optional callbacks of `router.push`: `onComplete` and `onAbort`. 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 `onComplete` and rejects instead of invoking `onAbort`. | ||
::: | ||
|
||
## Detecting Navigation Failures | ||
|
||
_Navigation Failures_ are `Error` instances with a few extra properties. To check if an error comes from the Router, use the `isNavigationFailure` function: | ||
|
||
```js | ||
import { NavigationFailureType, isNavigationFailure } from 'vue-router' | ||
|
||
// trying to access the admin page | ||
router.push('/admin').catch(failure => { | ||
if (isNavigationFailure(failure, NavigationFailureType.redirected)) { | ||
// show a small notification to the user | ||
showToast('Login in order to access the admin panel') | ||
} | ||
}) | ||
``` | ||
|
||
::: tip | ||
If you omit the second parameter: `isNavigationFailure(failure)`, it will only check if the error is a _Navigation Failure_. | ||
::: | ||
|
||
## `NavigationFailureType` | ||
|
||
`NavigationFailureType` help developers to differentiate between the various types of _Navigation Failures_. There are four different types: | ||
|
||
- `redirected`: `next(newLocation)` was called inside of a navigation guard to redirect somewhere else. | ||
- `aborted`: `next(false)` was called inside of a navigation guard to the navigation. | ||
- `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. | ||
- `duplicated`: The navigation was prevented because we are already at the target location. | ||
|
||
## _Navigation Failures_'s properties | ||
|
||
All navigation failures expose `to` and `from` properties to reflect the current location as well as the target location for the navigation that failed: | ||
|
||
```js | ||
// trying to access the admin page | ||
router.push('/admin').catch(failure => { | ||
if (isNavigationFailure(failure, NavigationFailureType.redirected)) { | ||
failure.to.path // '/admin' | ||
failure.from.path // '/' | ||
} | ||
}) | ||
``` | ||
|
||
In all cases, `to` and `from` are normalized route locations. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.