Skip to content

Commit 264dbd1

Browse files
committed
Add vm to beforeRouteUpdate's next callback
Fixes #1582
1 parent 7f1a6d8 commit 264dbd1

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/history/base.js

+21-12
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,15 @@ export class History {
112112
activated
113113
} = resolveQueue(this.current.matched, route.matched)
114114

115+
const postCbs = []
116+
const isValid = () => this.current === route
115117
const queue: Array<?NavigationGuard> = [].concat(
116118
// in-component leave guards
117119
extractLeaveGuards(deactivated),
118120
// global before hooks
119121
this.router.beforeHooks,
120122
// in-component update hooks
121-
extractUpdateHooks(updated),
123+
extractUpdateHooks(updated, postCbs, isValid),
122124
// in-config enter guards
123125
activated.map(m => m.beforeEnter),
124126
// async components
@@ -161,11 +163,9 @@ export class History {
161163
}
162164

163165
runQueue(queue, iterator, () => {
164-
const postEnterCbs = []
165-
const isValid = () => this.current === route
166166
// wait until async components are resolved before
167167
// extracting in-component enter guards
168-
const enterGuards = extractEnterGuards(activated, postEnterCbs, isValid)
168+
const enterGuards = extractEnterGuards(activated, postCbs, isValid)
169169
const queue = enterGuards.concat(this.router.resolveHooks)
170170
runQueue(queue, iterator, () => {
171171
if (this.pending !== route) {
@@ -175,7 +175,7 @@ export class History {
175175
onComplete(route)
176176
if (this.router.app) {
177177
this.router.app.$nextTick(() => {
178-
postEnterCbs.forEach(cb => { cb() })
178+
postCbs.forEach(cb => { cb() })
179179
})
180180
}
181181
})
@@ -266,10 +266,6 @@ function extractLeaveGuards (deactivated: Array<RouteRecord>): Array<?Function>
266266
return extractGuards(deactivated, 'beforeRouteLeave', bindGuard, true)
267267
}
268268

269-
function extractUpdateHooks (updated: Array<RouteRecord>): Array<?Function> {
270-
return extractGuards(updated, 'beforeRouteUpdate', bindGuard)
271-
}
272-
273269
function bindGuard (guard: NavigationGuard, instance: ?_Vue): ?NavigationGuard {
274270
if (instance) {
275271
return function boundRouteGuard () {
@@ -278,24 +274,37 @@ function bindGuard (guard: NavigationGuard, instance: ?_Vue): ?NavigationGuard {
278274
}
279275
}
280276

277+
function extractUpdateHooks (
278+
updated: Array<RouteRecord>,
279+
cbs: Array<Function>,
280+
isValid: () => boolean
281+
): Array<?Function> {
282+
return extractGuards(updated, 'beforeRouteUpdate', (guard, instance, match, key) => {
283+
var boundGuard = bindGuard(guard, instance)
284+
if (boundGuard) {
285+
return bindCbGuard(boundGuard, match, key, cbs, isValid)
286+
}
287+
})
288+
}
289+
281290
function extractEnterGuards (
282291
activated: Array<RouteRecord>,
283292
cbs: Array<Function>,
284293
isValid: () => boolean
285294
): Array<?Function> {
286295
return extractGuards(activated, 'beforeRouteEnter', (guard, _, match, key) => {
287-
return bindEnterGuard(guard, match, key, cbs, isValid)
296+
return bindCbGuard(guard, match, key, cbs, isValid)
288297
})
289298
}
290299

291-
function bindEnterGuard (
300+
function bindCbGuard (
292301
guard: NavigationGuard,
293302
match: RouteRecord,
294303
key: string,
295304
cbs: Array<Function>,
296305
isValid: () => boolean
297306
): NavigationGuard {
298-
return function routeEnterGuard (to, from, next) {
307+
return function (to, from, next) {
299308
return guard(to, from, cb => {
300309
next(cb)
301310
if (typeof cb === 'function') {

0 commit comments

Comments
 (0)