Skip to content

Commit

Permalink
setup hashchange properly to avoid regression regarding #725
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 1, 2016
1 parent 7fc6ffe commit 154e269
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
34 changes: 20 additions & 14 deletions src/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,21 @@ export class History {
this.cb = cb
}

transitionTo (location: RawLocation, cb?: Function) {
transitionTo (location: RawLocation, onComplete?: Function, onAbort?: Function) {
const route = this.router.match(location, this.current)
this.confirmTransition(route, () => {
this.updateRoute(route)
cb && cb(route)
onComplete && onComplete(route)
this.ensureURL()
})
}, onAbort)
}

confirmTransition (route: Route, cb: Function) {
confirmTransition (route: Route, onComplete: Function, onAbort?: Function) {
const current = this.current
const abort = () => { onAbort && onAbort() }
if (isSameRoute(route, current)) {
this.ensureURL()
return
return abort()
}

const {
Expand All @@ -66,14 +67,18 @@ export class History {

this.pending = route
const iterator = (hook: NavigationGuard, next) => {
if (this.pending !== route) return
if (this.pending !== route) {
return abort()
}
hook(route, current, (to: any) => {
if (to === false) {
// next(false) -> abort navigation, ensure current URL
this.ensureURL(true)
abort()
} else if (typeof to === 'string' || typeof to === 'object') {
// next('/') or next({ path: '/' }) -> redirect
(typeof to === 'object' && to.replace) ? this.replace(to) : this.push(to)
abort()
} else {
// confirm transition and pass on the value
next(to)
Expand All @@ -89,14 +94,15 @@ export class History {
// wait until async components are resolved before
// extracting in-component enter guards
runQueue(enterGuards, iterator, () => {
if (this.pending === route) {
this.pending = null
cb(route)
if (this.router.app) {
this.router.app.$nextTick(() => {
postEnterCbs.forEach(cb => cb())
})
}
if (this.pending !== route) {
return abort()
}
this.pending = null
onComplete(route)
if (this.router.app) {
this.router.app.$nextTick(() => {
postEnterCbs.forEach(cb => cb())
})
}
})
})
Expand Down
5 changes: 0 additions & 5 deletions src/history/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@ import { cleanPath } from '../util/path'
export class HashHistory extends History {
constructor (router: VueRouter, base: ?string, fallback: boolean) {
super(router, base)
window.addEventListener('hashchange', () => {
this.onHashChange()
})

// check history fallback deeplinking
if (fallback && this.checkFallback()) {
return
}

ensureSlash()
}

Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ export default class VueRouter {
if (history instanceof HTML5History) {
history.transitionTo(getLocation(history.base))
} else if (history instanceof HashHistory) {
history.transitionTo(getHash())
const setupHashListener = () => {
window.addEventListener('hashchange', () => {
history.onHashChange()
})
}
history.transitionTo(getHash(), setupHashListener, setupHashListener)
}

history.listen(route => {
Expand Down

0 comments on commit 154e269

Please sign in to comment.