Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class History {
readyCbs: Array<Function>
readyErrorCbs: Array<Function>
errorCbs: Array<Function>
removeEventListenerCbs: Array<Function>

// implemented by sub-classes
+go: (n: number) => void
Expand All @@ -41,6 +42,7 @@ export class History {
this.readyCbs = []
this.readyErrorCbs = []
this.errorCbs = []
this.removeEventListenerCbs = []
}

listen (cb: Function) {
Expand Down Expand Up @@ -208,6 +210,10 @@ export class History {
hook && hook(route, prev)
})
}

destroy () {
this.removeEventListenerCbs.forEach(cb => { cb() })
}
}

function normalizeBase (base: ?string): string {
Expand Down
38 changes: 21 additions & 17 deletions src/history/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,30 @@ export class HashHistory extends History {
const supportsScroll = supportsPushState && expectScroll

if (supportsScroll) {
setupScroll()
const uninstall = setupScroll()
this.removeEventListenerCbs.push(uninstall)
}

window.addEventListener(
supportsPushState ? 'popstate' : 'hashchange',
() => {
const current = this.current
if (!ensureSlash()) {
return
}
this.transitionTo(getHash(), route => {
if (supportsScroll) {
handleScroll(this.router, route, current, true)
}
if (!supportsPushState) {
replaceHash(route.fullPath)
}
})
const eventName = supportsPushState ? 'popstate' : 'hashchange'
const listener = () => {
const current = this.current
if (!ensureSlash()) {
return
}
)
this.transitionTo(getHash(), route => {
if (supportsScroll) {
handleScroll(this.router, route, current, true)
}
if (!supportsPushState) {
replaceHash(route.fullPath)
}
})
}
window.addEventListener(eventName, listener)
const uninstall = () => {
window.removeEventListener(eventName, listener)
}
this.removeEventListenerCbs.push(uninstall)
}

push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
Expand Down
12 changes: 9 additions & 3 deletions src/history/html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ export class HTML5History extends History {
const supportsScroll = supportsPushState && expectScroll

if (supportsScroll) {
setupScroll()
const uninstall = setupScroll()
this.removeEventListenerCbs.push(uninstall)
}

const initLocation = getLocation(this.base)
window.addEventListener('popstate', e => {
const listener = e => {
const current = this.current

// Avoiding first `popstate` event dispatched in some browsers but first
Expand All @@ -34,7 +35,12 @@ export class HTML5History extends History {
handleScroll(router, route, current, true)
}
})
})
}
window.addEventListener('popstate', listener)
const uninstall = () => {
window.removeEventListener('popstate', listener)
}
this.removeEventListenerCbs.push(uninstall)
}

go (n: number) {
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ export default class VueRouter {
this.history.transitionTo(this.history.getCurrentLocation())
}
}

destroy () {
this.history.destroy()
}
}

function registerHook (list: Array<any>, fn: Function): Function {
Expand Down
9 changes: 7 additions & 2 deletions src/util/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ export function setupScroll () {
const protocolAndPath = window.location.protocol + '//' + window.location.host
const absolutePath = window.location.href.replace(protocolAndPath, '')
window.history.replaceState({ key: getStateKey() }, '', absolutePath)
window.addEventListener('popstate', e => {
const listener = e => {
saveScrollPosition()
if (e.state && e.state.key) {
setStateKey(e.state.key)
}
})
}
window.addEventListener('popstate', listener)

return function uninstall () {
window.removeEventListener('popstate', listener)
}
}

export function handleScroll (
Expand Down