Skip to content

Commit 154e269

Browse files
committed
setup hashchange properly to avoid regression regarding #725
1 parent 7fc6ffe commit 154e269

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

Diff for: src/history/base.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,21 @@ export class History {
3232
this.cb = cb
3333
}
3434

35-
transitionTo (location: RawLocation, cb?: Function) {
35+
transitionTo (location: RawLocation, onComplete?: Function, onAbort?: Function) {
3636
const route = this.router.match(location, this.current)
3737
this.confirmTransition(route, () => {
3838
this.updateRoute(route)
39-
cb && cb(route)
39+
onComplete && onComplete(route)
4040
this.ensureURL()
41-
})
41+
}, onAbort)
4242
}
4343

44-
confirmTransition (route: Route, cb: Function) {
44+
confirmTransition (route: Route, onComplete: Function, onAbort?: Function) {
4545
const current = this.current
46+
const abort = () => { onAbort && onAbort() }
4647
if (isSameRoute(route, current)) {
4748
this.ensureURL()
48-
return
49+
return abort()
4950
}
5051

5152
const {
@@ -66,14 +67,18 @@ export class History {
6667

6768
this.pending = route
6869
const iterator = (hook: NavigationGuard, next) => {
69-
if (this.pending !== route) return
70+
if (this.pending !== route) {
71+
return abort()
72+
}
7073
hook(route, current, (to: any) => {
7174
if (to === false) {
7275
// next(false) -> abort navigation, ensure current URL
7376
this.ensureURL(true)
77+
abort()
7478
} else if (typeof to === 'string' || typeof to === 'object') {
7579
// next('/') or next({ path: '/' }) -> redirect
7680
(typeof to === 'object' && to.replace) ? this.replace(to) : this.push(to)
81+
abort()
7782
} else {
7883
// confirm transition and pass on the value
7984
next(to)
@@ -89,14 +94,15 @@ export class History {
8994
// wait until async components are resolved before
9095
// extracting in-component enter guards
9196
runQueue(enterGuards, iterator, () => {
92-
if (this.pending === route) {
93-
this.pending = null
94-
cb(route)
95-
if (this.router.app) {
96-
this.router.app.$nextTick(() => {
97-
postEnterCbs.forEach(cb => cb())
98-
})
99-
}
97+
if (this.pending !== route) {
98+
return abort()
99+
}
100+
this.pending = null
101+
onComplete(route)
102+
if (this.router.app) {
103+
this.router.app.$nextTick(() => {
104+
postEnterCbs.forEach(cb => cb())
105+
})
100106
}
101107
})
102108
})

Diff for: src/history/hash.js

-5
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@ import { cleanPath } from '../util/path'
88
export class HashHistory extends History {
99
constructor (router: VueRouter, base: ?string, fallback: boolean) {
1010
super(router, base)
11-
window.addEventListener('hashchange', () => {
12-
this.onHashChange()
13-
})
14-
1511
// check history fallback deeplinking
1612
if (fallback && this.checkFallback()) {
1713
return
1814
}
19-
2015
ensureSlash()
2116
}
2217

Diff for: src/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ export default class VueRouter {
7272
if (history instanceof HTML5History) {
7373
history.transitionTo(getLocation(history.base))
7474
} else if (history instanceof HashHistory) {
75-
history.transitionTo(getHash())
75+
const setupHashListener = () => {
76+
window.addEventListener('hashchange', () => {
77+
history.onHashChange()
78+
})
79+
}
80+
history.transitionTo(getHash(), setupHashListener, setupHashListener)
7681
}
7782

7883
history.listen(route => {

0 commit comments

Comments
 (0)