Skip to content

Commit 7731488

Browse files
committed
return unregister fn from router hook register methods
1 parent de75721 commit 7731488

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

flow/declarations.js

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ declare type NavigationGuard = (
1515
next: (to?: RawLocation | false | Function | void) => void
1616
) => any
1717

18+
declare type AfterNavigationHook = (to: Route, from: Route) => any
19+
1820
declare type RouterOptions = {
1921
routes?: Array<RouteConfig>;
2022
mode?: string;

src/index.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default class VueRouter {
3030
fallback: boolean;
3131
beforeHooks: Array<?NavigationGuard>;
3232
resolveHooks: Array<?NavigationGuard>;
33-
afterHooks: Array<?((to: Route, from: Route) => any)>;
33+
afterHooks: Array<?AfterNavigationHook>;
3434

3535
constructor (options: RouterOptions = {}) {
3636
this.app = null
@@ -118,16 +118,16 @@ export default class VueRouter {
118118
})
119119
}
120120

121-
beforeEach (fn: Function) {
122-
this.beforeHooks.push(fn)
121+
beforeEach (fn: Function): Function {
122+
return registerHook(this.beforeHooks, fn)
123123
}
124124

125-
beforeResolve (fn: Function) {
126-
this.resolveHooks.push(fn)
125+
beforeResolve (fn: Function): Function {
126+
return registerHook(this.resolveHooks, fn)
127127
}
128128

129-
afterEach (fn: Function) {
130-
this.afterHooks.push(fn)
129+
afterEach (fn: Function): Function {
130+
return registerHook(this.afterHooks, fn)
131131
}
132132

133133
onReady (cb: Function, errorCb?: Function) {
@@ -212,6 +212,14 @@ export default class VueRouter {
212212
}
213213
}
214214

215+
function registerHook (list: Array<any>, fn: Function): Function {
216+
list.push(fn)
217+
return () => {
218+
const i = list.indexOf(fn)
219+
if (i > -1) list.splice(i, 1)
220+
}
221+
}
222+
215223
function createHref (base: string, fullPath: string, mode) {
216224
var path = mode === 'hash' ? '#' + fullPath : fullPath
217225
return base ? cleanPath(base + '/' + path) : path

types/router.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ declare class VueRouter {
2020
mode: RouterMode;
2121
currentRoute: Route;
2222

23-
beforeEach (guard: NavigationGuard): void;
24-
beforeResolve (guard: NavigationGuard): void;
25-
afterEach (hook: (to: Route, from: Route) => any): void;
23+
beforeEach (guard: NavigationGuard): Function;
24+
beforeResolve (guard: NavigationGuard): Function;
25+
afterEach (hook: (to: Route, from: Route) => any): Function;
2626
push (location: RawLocation, onComplete?: Function, onAbort?: Function): void;
2727
replace (location: RawLocation, onComplete?: Function, onAbort?: Function): void;
2828
go (n: number): void;

types/test/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,20 @@ matched.forEach(m => {
111111
const redirect: RedirectOption | undefined = m.redirect;
112112
});
113113

114-
router.beforeEach((to, from, next) => {
114+
const unregister = router.beforeEach((to, from, next) => {
115115
to.params;
116116
next("/");
117117
next();
118118
});
119119

120+
unregister();
121+
122+
router.beforeResolve((to, from, next) => {
123+
to.params;
124+
from.params;
125+
next()
126+
});
127+
120128
router.afterEach((to, from) => {
121129
to.params;
122130
from.params;

0 commit comments

Comments
 (0)