Skip to content

Commit 83418bc

Browse files
fnlctrlyyx990803
authored andcommitted
Allow calling router instance methods before vue instantiation. (fix #795)
Previously it would throw errors. Allowed methods: push, replace, go, back, forward Reason: These methods only try to manipulate browser history, and should work even when root vue instance has not been created. Common use case: Call `router.replace()` to set browser url state. This action doesn't require vue's existence, so it shouldn't throw an error when called before vue's instantiation. Otherwise, users may need to check router's actual mode after fallback (history or hash) and then call `history.replaceState` or `location.replace` accordingly.
1 parent 8f80641 commit 83418bc

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

src/history/base.js

+8
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ export class History {
108108
hook && hook(route, prev)
109109
})
110110
}
111+
112+
/**
113+
* Dummy method to make flow happy...
114+
* otherwise it won't let me call `this.history.onHashChange`
115+
* inside a `switch case`...
116+
* Advices on how to remove this method are much appreciated.
117+
*/
118+
onHashChange () {}
111119
}
112120

113121
function normalizeBase (base: ?string): string {

src/history/hash.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ export class HashHistory extends History {
1515
}
1616

1717
ensureSlash()
18-
this.transitionTo(getHash(), () => {
19-
window.addEventListener('hashchange', () => {
20-
this.onHashChange()
21-
})
22-
})
2318
}
2419

2520
checkFallback () {
@@ -73,7 +68,7 @@ function ensureSlash (): boolean {
7368
return false
7469
}
7570

76-
function getHash (): string {
71+
export function getHash (): string {
7772
// We can't use window.location.hash here because it's not
7873
// consistent across browsers - Firefox will pre-decode it!
7974
const href = window.location.href

src/history/html5.js

-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export class HTML5History extends History {
1919
constructor (router: VueRouter, base: ?string) {
2020
super(router, base)
2121

22-
this.transitionTo(getLocation(this.base))
23-
2422
const expectScroll = router.options.scrollBehavior
2523
window.addEventListener('popstate', e => {
2624
_key = e.state && e.state.key

src/index.js

+23-11
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import { install } from './install'
44
import { createMatcher } from './create-matcher'
5-
import { HashHistory } from './history/hash'
6-
import { HTML5History } from './history/html5'
5+
import { HashHistory, getHash } from './history/hash'
6+
import { HTML5History, getLocation } from './history/html5'
77
import { AbstractHistory } from './history/abstract'
88
import { inBrowser, supportsHistory } from './util/dom'
99
import { assert } from './util/warn'
@@ -36,6 +36,20 @@ export default class VueRouter {
3636
mode = 'abstract'
3737
}
3838
this.mode = mode
39+
40+
switch (mode) {
41+
case 'history':
42+
this.history = new HTML5History(this, options.base)
43+
break
44+
case 'hash':
45+
this.history = new HashHistory(this, options.base, this.fallback)
46+
break
47+
case 'abstract':
48+
this.history = new AbstractHistory(this)
49+
break
50+
default:
51+
assert(false, `invalid mode: ${mode}`)
52+
}
3953
}
4054

4155
get currentRoute (): ?Route {
@@ -51,19 +65,17 @@ export default class VueRouter {
5165

5266
this.app = app
5367

54-
const { mode, options, fallback } = this
55-
switch (mode) {
68+
switch (this.mode) {
5669
case 'history':
57-
this.history = new HTML5History(this, options.base)
70+
this.history.transitionTo(getLocation(this.history.base))
5871
break
5972
case 'hash':
60-
this.history = new HashHistory(this, options.base, fallback)
73+
this.history.transitionTo(getHash(), () => {
74+
window.addEventListener('hashchange', () => {
75+
this.history.onHashChange()
76+
})
77+
})
6178
break
62-
case 'abstract':
63-
this.history = new AbstractHistory(this)
64-
break
65-
default:
66-
assert(false, `invalid mode: ${mode}`)
6779
}
6880

6981
this.history.listen(route => {

0 commit comments

Comments
 (0)