Skip to content

Commit 80c4a38

Browse files
committed
update type declarations for navigation guard signature change
1 parent 63efb0c commit 80c4a38

File tree

8 files changed

+41
-47
lines changed

8 files changed

+41
-47
lines changed

flow/declarations.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ declare module 'path-to-regexp' {
77

88
declare type Dictionary<T> = { [key: string]: T }
99

10+
declare type NavigationGuard = (
11+
to: Route,
12+
from: Route,
13+
next: (to?: RawLocation | false | Function | void) => void
14+
) => any
15+
1016
declare type RouterOptions = {
1117
routes?: Array<RouteConfig>;
1218
mode?: string;

src/history/base.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class History {
5252
activated
5353
} = resolveQueue(this.current.matched, route.matched)
5454

55-
const queue = [].concat(
55+
const queue: Array<?NavigationGuard> = [].concat(
5656
// in-component leave guards
5757
extractLeaveGuards(deactivated),
5858
// global before hooks
@@ -64,7 +64,7 @@ export class History {
6464
)
6565

6666
this.pending = route
67-
const iterator = (hook, next) => {
67+
const iterator = (hook: NavigationGuard, next) => {
6868
if (this.pending !== route) return
6969
hook(route, current, (to: any) => {
7070
if (to === false) {

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export default class VueRouter {
1717
history: HashHistory | HTML5History | AbstractHistory;
1818
match: Matcher;
1919
fallback: boolean;
20-
beforeHooks: Array<?Function>;
21-
afterHooks: Array<?Function>;
20+
beforeHooks: Array<?NavigationGuard>;
21+
afterHooks: Array<?((to: Route, from: Route) => any)>;
2222

2323
constructor (options: RouterOptions = {}) {
2424
this.app = null

src/util/async.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
22

3-
export function runQueue (queue: Array<any>, fn: Function, cb: Function) {
3+
export function runQueue (queue: Array<?NavigationGuard>, fn: Function, cb: Function) {
44
const step = index => {
55
if (index >= queue.length) {
66
cb()

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ declare namespace VueRouter {
1212
export type RouteRecord = R.RouteRecord;
1313
export type Location = R.Location;
1414
export type Route = R.Route;
15+
export type NavigationGuard = R.NavigationGuard;
1516
}
1617

1718
// TS cannot merge imported class with namespace, declare a subclass to bypass

types/router.d.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ type Dictionary<T> = { [key: string]: T };
77
export type RouterMode = "hash" | "history" | "abstract";
88
export type RawLocation = string | Location;
99
export type RedirectOption = RawLocation | ((to: Route) => RawLocation);
10+
export type NavigationGuard = (
11+
to: Route,
12+
from: Route,
13+
next: (to?: RawLocation | false | ((vm: Vue) => any) | void) => void
14+
) => any
1015

1116
declare class VueRouter {
1217
constructor (options?: RouterOptions);
@@ -15,14 +20,8 @@ declare class VueRouter {
1520
mode: RouterMode;
1621
currentRoute: Route;
1722

18-
beforeEach (
19-
guard: (
20-
route: Route,
21-
redirect: (location: RawLocation) => void,
22-
next: () => void
23-
) => any
24-
): void;
25-
afterEach (hook: (route: Route) => any): void;
23+
beforeEach (guard: NavigationGuard): void;
24+
afterEach (hook: (to: Route, from: Route) => any): void;
2625
push (location: RawLocation): void;
2726
replace (location: RawLocation): void;
2827
go (n: number): void;
@@ -54,11 +53,7 @@ export interface RouteConfig {
5453
alias?: string | string[];
5554
children?: RouteConfig[];
5655
meta?: any;
57-
beforeEnter?: (
58-
route: Route,
59-
redirect: (location: RawLocation) => void,
60-
next: () => void
61-
) => any;
56+
beforeEnter?: NavigationGuard;
6257
}
6358

6459
export interface RouteRecord {

types/test/index.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@ const Home = { template: "<div>home</div>" };
1010
const Foo = { template: "<div>foo</div>" };
1111
const Bar = { template: "<div>bar</div>" };
1212

13-
const Hook: ComponentOptions<{ a: string } & Vue> = {
13+
const Hook: ComponentOptions<Vue> = {
1414
template: "<div>hook</div>",
1515

16-
beforeRouteEnter (route, redirect, next) {
16+
beforeRouteEnter (to, from, next) {
1717
route.params;
18-
redirect("/");
18+
next("/");
19+
next({ path: "/" });
1920
next(vm => {
20-
vm.a = "done";
21+
vm.$router;
2122
});
2223
},
2324

24-
beforeRouteLeave (route, redirect, next) {
25+
beforeRouteLeave (to, from, next) {
2526
route.params;
26-
redirect("/");
27+
next("/");
28+
next({ path: "/" });
2729
next();
2830
}
2931
};
@@ -54,9 +56,10 @@ const router = new VueRouter({
5456
bar: Bar
5557
},
5658
meta: { auth: true },
57-
beforeEnter (route, redirect, next) {
58-
route.params;
59-
redirect({ name: "home" });
59+
beforeEnter (to, from, next) {
60+
to.params;
61+
from.params;
62+
next({ name: "home" });
6063
next();
6164
}
6265
},
@@ -98,14 +101,15 @@ matched.forEach(m => {
98101
const redirect: RedirectOption | undefined = m.redirect;
99102
});
100103

101-
router.beforeEach((route, redirect, next) => {
102-
route.params;
103-
redirect("/");
104+
router.beforeEach((to, from, next) => {
105+
to.params;
106+
next("/");
104107
next();
105108
});
106109

107-
router.afterEach(route => {
108-
route.params;
110+
router.afterEach((to, from) => {
111+
to.params;
112+
from.params;
109113
});
110114

111115
router.push({

types/vue.d.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import Vue = require("vue");
66
import VueRouter = require("./index");
7-
import { Route, RawLocation } from "./index";
7+
import { Route, RawLocation, NavigationGuard } from "./index";
88

99
declare module "vue/types/vue" {
1010
interface Vue {
@@ -16,19 +16,7 @@ declare module "vue/types/vue" {
1616
declare module "vue/types/options" {
1717
interface ComponentOptions<V extends Vue> {
1818
router?: VueRouter;
19-
20-
beforeRouteEnter?: (
21-
this: never,
22-
route: Route,
23-
redirect: (location: RawLocation) => void,
24-
next: (callback: (vm: V) => any) => void
25-
) => any;
26-
27-
beforeRouteLeave?: (
28-
this: V,
29-
route: Route,
30-
redirect: (location: RawLocation) => void,
31-
next: () => void
32-
) => any;
19+
beforeRouteEnter?: NavigationGuard;
20+
beforeRouteLeave?: NavigationGuard;
3321
}
3422
}

0 commit comments

Comments
 (0)