Skip to content

Commit ecc8e27

Browse files
committed
fix(types): fix VueRouter.NavigationFailureType
1 parent c69ff7b commit ecc8e27

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

docs/guide/advanced/navigation-failures.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ When using `router-link`, Vue Router calls `router.push` to trigger a navigation
1111
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_.
1212

1313
::: tip Background story
14-
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`.
15-
:::
14+
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`.
15+
:::
1616

1717
## Detecting Navigation Failures
1818

1919
_Navigation Failures_ are `Error` instances with a few extra properties. To check if an error comes from the Router, use the `isNavigationFailure` function:
2020

2121
```js
22-
import { NavigationFailureType, isNavigationFailure } from 'vue-router'
22+
import VueRouter from 'vue-router'
23+
const { isNavigationFailure, NavigationFailureType } = VueRouter
2324

2425
// trying to access the admin page
2526
router.push('/admin').catch(failure => {

test/unit/specs/error-handling.spec.js

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ describe('error handling', () => {
5757
router.push('/foo')
5858
router.push('/foo').catch(err => {
5959
expect(err.type).toBe(NavigationFailureType.duplicated)
60+
expect(VueRouter.isNavigationFailure(err)).toBe(true)
61+
expect(VueRouter.isNavigationFailure(err, NavigationFailureType.duplicated)).toBe(true)
6062
done()
6163
})
6264
})

types/router.d.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,16 @@ export declare class VueRouter {
6767
error: any,
6868
type?: number
6969
) => error is NavigationFailure
70-
static NavigationFailureType: NavigationFailureType
70+
static NavigationFailureType: {
71+
[k in keyof typeof NavigationFailureType]: NavigationFailureType
72+
}
7173
}
7274

7375
export enum NavigationFailureType {
74-
redirected= 2,
75-
aborted= 4,
76-
cancelled= 8,
77-
duplicated= 16
76+
redirected = 2,
77+
aborted = 4,
78+
cancelled = 8,
79+
duplicated = 16
7880
}
7981

8082
export interface NavigationFailure extends Error {

types/test/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const Abc = { template: '<div>abc</div>' }
1818
const Async = () => Promise.resolve({ template: '<div>async</div>' })
1919

2020
let err: any
21-
if (VueRouter.isNavigationFailure(err, NavigationFailureType.aborted)) {
21+
if (VueRouter.isNavigationFailure(err, VueRouter.NavigationFailureType.aborted)) {
2222
err.from.fullPath.split('/')
2323
}
2424

0 commit comments

Comments
 (0)