Skip to content

Commit fda7067

Browse files
committedAug 13, 2020
fix(types): add missing NavigationFailure types
Close #3293
1 parent 3047798 commit fda7067

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed
 

‎src/util/errors.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// When changing thing, also edit router.d.ts
12
export const NavigationFailureType = {
23
redirected: 2,
34
aborted: 4,

‎types/index.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ export {
1313
Location,
1414
Route,
1515
NavigationGuard,
16-
NavigationGuardNext
16+
NavigationGuardNext,
17+
NavigationFailureType,
18+
NavigationFailure
1719
} from './router'

‎types/router.d.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export declare class VueRouter {
2121
constructor(options?: RouterOptions)
2222

2323
app: Vue
24-
options: RouterOptions;
24+
options: RouterOptions
2525
mode: RouterMode
2626
currentRoute: Route
2727

@@ -63,15 +63,24 @@ export declare class VueRouter {
6363
static install: PluginFunction<never>
6464
static version: string
6565

66-
static isNavigationFailure: (error: any, type?: NavigationFailureTypeE) => error is Error
67-
static NavigationFailureType: NavigationFailureTypeE
66+
static isNavigationFailure: (
67+
error: any,
68+
type?: number
69+
) => error is NavigationFailure
70+
static NavigationFailureType: NavigationFailureType
6871
}
6972

70-
export enum NavigationFailureTypeE {
71-
redirected = 1,
72-
aborted = 2,
73-
cancelled = 3,
74-
duplicated = 4
73+
export enum NavigationFailureType {
74+
redirected= 2,
75+
aborted= 4,
76+
cancelled= 8,
77+
duplicated= 16
78+
}
79+
80+
export interface NavigationFailure extends Error {
81+
to: Route
82+
from: Route
83+
type: number
7584
}
7685

7786
type Position = { x: number; y: number }

‎types/test/index.ts

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import Vue, { ComponentOptions, AsyncComponent } from 'vue'
22

33
import VueRouter from '../index'
4-
import { Route, RouteRecord, RedirectOption } from '../index'
4+
import {
5+
Route,
6+
RouteRecord,
7+
RedirectOption,
8+
NavigationFailure,
9+
NavigationFailureType
10+
} from '../index'
511

612
Vue.use(VueRouter)
713

@@ -11,6 +17,14 @@ const Bar = { template: '<div>bar</div>' }
1117
const Abc = { template: '<div>abc</div>' }
1218
const Async = () => Promise.resolve({ template: '<div>async</div>' })
1319

20+
let err: any
21+
if (VueRouter.isNavigationFailure(err, NavigationFailureType.aborted)) {
22+
err.from.fullPath.split('/')
23+
}
24+
25+
let navigationFailure = new Error() as NavigationFailure
26+
navigationFailure.to.fullPath.split('/')
27+
1428
const Hook: ComponentOptions<Vue> = {
1529
template: '<div>hook</div>',
1630

@@ -181,8 +195,16 @@ router.push({
181195
})
182196
router.replace({ name: 'home' })
183197

184-
router.push('/', () => {}, () => {})
185-
router.replace('/foo', () => {}, () => {})
198+
router.push(
199+
'/',
200+
() => {},
201+
() => {}
202+
)
203+
router.replace(
204+
'/foo',
205+
() => {},
206+
() => {}
207+
)
186208

187209
// promises
188210

@@ -204,7 +226,8 @@ router.forward()
204226
const Components: (
205227
| ComponentOptions<Vue>
206228
| typeof Vue
207-
| AsyncComponent)[] = router.getMatchedComponents()
229+
| AsyncComponent
230+
)[] = router.getMatchedComponents()
208231

209232
const vm = new Vue({
210233
router,

0 commit comments

Comments
 (0)