diff --git a/src/util/errors.js b/src/util/errors.js index bacf6881e..a61f6eea3 100644 --- a/src/util/errors.js +++ b/src/util/errors.js @@ -1,3 +1,4 @@ +// When changing thing, also edit router.d.ts export const NavigationFailureType = { redirected: 2, aborted: 4, diff --git a/types/index.d.ts b/types/index.d.ts index 649303649..504f2b56a 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -13,5 +13,7 @@ export { Location, Route, NavigationGuard, - NavigationGuardNext + NavigationGuardNext, + NavigationFailureType, + NavigationFailure } from './router' diff --git a/types/router.d.ts b/types/router.d.ts index 07f912eb6..49da2a7f3 100644 --- a/types/router.d.ts +++ b/types/router.d.ts @@ -21,7 +21,7 @@ export declare class VueRouter { constructor(options?: RouterOptions) app: Vue - options: RouterOptions; + options: RouterOptions mode: RouterMode currentRoute: Route @@ -63,15 +63,24 @@ export declare class VueRouter { static install: PluginFunction static version: string - static isNavigationFailure: (error: any, type?: NavigationFailureTypeE) => error is Error - static NavigationFailureType: NavigationFailureTypeE + static isNavigationFailure: ( + error: any, + type?: number + ) => error is NavigationFailure + static NavigationFailureType: NavigationFailureType } -export enum NavigationFailureTypeE { - redirected = 1, - aborted = 2, - cancelled = 3, - duplicated = 4 +export enum NavigationFailureType { + redirected= 2, + aborted= 4, + cancelled= 8, + duplicated= 16 +} + +export interface NavigationFailure extends Error { + to: Route + from: Route + type: number } type Position = { x: number; y: number } diff --git a/types/test/index.ts b/types/test/index.ts index 382953c19..c8e8331ff 100644 --- a/types/test/index.ts +++ b/types/test/index.ts @@ -1,7 +1,13 @@ import Vue, { ComponentOptions, AsyncComponent } from 'vue' import VueRouter from '../index' -import { Route, RouteRecord, RedirectOption } from '../index' +import { + Route, + RouteRecord, + RedirectOption, + NavigationFailure, + NavigationFailureType +} from '../index' Vue.use(VueRouter) @@ -11,6 +17,14 @@ const Bar = { template: '
bar
' } const Abc = { template: '
abc
' } const Async = () => Promise.resolve({ template: '
async
' }) +let err: any +if (VueRouter.isNavigationFailure(err, NavigationFailureType.aborted)) { + err.from.fullPath.split('/') +} + +let navigationFailure = new Error() as NavigationFailure +navigationFailure.to.fullPath.split('/') + const Hook: ComponentOptions = { template: '
hook
', @@ -181,8 +195,16 @@ router.push({ }) router.replace({ name: 'home' }) -router.push('/', () => {}, () => {}) -router.replace('/foo', () => {}, () => {}) +router.push( + '/', + () => {}, + () => {} +) +router.replace( + '/foo', + () => {}, + () => {} +) // promises @@ -204,7 +226,8 @@ router.forward() const Components: ( | ComponentOptions | typeof Vue - | AsyncComponent)[] = router.getMatchedComponents() + | AsyncComponent +)[] = router.getMatchedComponents() const vm = new Vue({ router,