diff --git a/src/history/base.js b/src/history/base.js index 40cccfe6a..95d42cce2 100644 --- a/src/history/base.js +++ b/src/history/base.js @@ -17,7 +17,7 @@ export class History { go: (n: number) => void; push: (loc: RawLocation) => void; replace: (loc: RawLocation) => void; - ensureURL: () => void; + ensureURL: (push?: boolean) => void; constructor (router: VueRouter, base: ?string) { this.router = router @@ -69,7 +69,7 @@ export class History { hook(route, current, (to: any) => { if (to === false) { // next(false) -> abort navigation, ensure current URL - this.ensureURL() + this.ensureURL(true) } else if (typeof to === 'string' || typeof to === 'object') { // next('/') or next({ path: '/' }) -> redirect this.push(to) diff --git a/src/history/hash.js b/src/history/hash.js index c13466477..667e0129d 100644 --- a/src/history/hash.js +++ b/src/history/hash.js @@ -57,9 +57,10 @@ export class HashHistory extends History { window.history.go(n) } - ensureURL () { - if (getHash() !== this.current.fullPath) { - replaceHash(this.current.fullPath) + ensureURL (push?: boolean) { + const current = this.current.fullPath + if (getHash() !== current) { + push ? pushHash(current) : replaceHash(current) } } } diff --git a/src/history/html5.js b/src/history/html5.js index f7b9fbba7..4b28b0503 100644 --- a/src/history/html5.js +++ b/src/history/html5.js @@ -59,9 +59,10 @@ export class HTML5History extends History { }) } - ensureURL () { + ensureURL (push?: boolean) { if (getLocation(this.base) !== this.current.fullPath) { - replaceState(cleanPath(this.base + this.current.fullPath)) + const current = cleanPath(this.base + this.current.fullPath) + push ? pushState(current) : replaceState(current) } }