Skip to content

Commit 5613e15

Browse files
committed
start history when root instance is created
1 parent 18546f2 commit 5613e15

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/index.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,35 @@ export default class VueRouter {
1313

1414
app: any;
1515
options: RouterOptions;
16-
mode: 'hash' | 'history' | 'abstract';
16+
mode: string;
1717
history: HashHistory | HTML5History | AbstractHistory;
1818
match: Matcher;
19+
fallback: boolean;
1920

2021
constructor (options: RouterOptions = {}) {
21-
assert(
22-
install.installed,
23-
`not installed. Make sure to call \`Vue.use(VueRouter)\` ` +
24-
`before mounting root instance.`
25-
)
26-
2722
this.app = null
2823
this.options = options
2924
this.match = createMatcher(options.routes || [])
3025

3126
let mode = options.mode || 'hash'
32-
const fallback = mode === 'history' && !supportsHistory
33-
if (fallback) {
27+
this.fallback = mode === 'history' && !supportsHistory
28+
if (this.fallback) {
3429
mode = 'hash'
3530
}
3631
if (!inBrowser) {
3732
mode = 'abstract'
3833
}
34+
this.mode = mode
35+
}
3936

37+
init (app: any /* Vue component instance */) {
38+
assert(
39+
install.installed,
40+
`not installed. Make sure to call \`Vue.use(VueRouter)\` ` +
41+
`before creating root instance.`
42+
)
43+
44+
const { mode, options, fallback } = this
4045
switch (mode) {
4146
case 'history':
4247
this.history = new HTML5History(this, options.base)
@@ -51,8 +56,7 @@ export default class VueRouter {
5156
assert(false, `invalid mode: ${mode}`)
5257
}
5358

54-
this.mode = mode
55-
59+
this.app = app
5660
this.history.listen(route => {
5761
this.app._route = route
5862
})

src/install.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function install (Vue) {
1717
beforeCreate () {
1818
if (this.$options.router) {
1919
this._router = this.$options.router
20-
this._router.app = this
20+
this._router.init(this)
2121
Vue.util.defineReactive(this, '_route', this._router.history.current)
2222
}
2323
}

0 commit comments

Comments
 (0)