Skip to content

Allow calling instance methods before vue instantiation. (fix #795) #797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
8 changes: 8 additions & 0 deletions src/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ export class History {
hook && hook(route, prev)
})
}

/**
* Dummy method to make flow happy...
* otherwise it won't let me call `this.history.onHashChange`
* inside a `switch case`...
* Advices on how to remove this method are much appreciated.
*/
onHashChange () {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ help needed.

}

function normalizeBase (base: ?string): string {
Expand Down
7 changes: 1 addition & 6 deletions src/history/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ export class HashHistory extends History {
}

ensureSlash()
this.transitionTo(getHash(), () => {
window.addEventListener('hashchange', () => {
this.onHashChange()
})
})
}

checkFallback () {
Expand Down Expand Up @@ -73,7 +68,7 @@ function ensureSlash (): boolean {
return false
}

function getHash (): string {
export function getHash (): string {
// We can't use window.location.hash here because it's not
// consistent across browsers - Firefox will pre-decode it!
const href = window.location.href
Expand Down
2 changes: 0 additions & 2 deletions src/history/html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export class HTML5History extends History {
constructor (router: VueRouter, base: ?string) {
super(router, base)

this.transitionTo(getLocation(this.base))

const expectScroll = router.options.scrollBehavior
window.addEventListener('popstate', e => {
_key = e.state && e.state.key
Expand Down
34 changes: 23 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import { install } from './install'
import { createMatcher } from './create-matcher'
import { HashHistory } from './history/hash'
import { HTML5History } from './history/html5'
import { HashHistory, getHash } from './history/hash'
import { HTML5History, getLocation } from './history/html5'
import { AbstractHistory } from './history/abstract'
import { inBrowser, supportsHistory } from './util/dom'
import { assert } from './util/warn'
Expand Down Expand Up @@ -36,6 +36,20 @@ export default class VueRouter {
mode = 'abstract'
}
this.mode = mode

switch (mode) {
case 'history':
this.history = new HTML5History(this, options.base)
break
case 'hash':
this.history = new HashHistory(this, options.base, this.fallback)
break
case 'abstract':
this.history = new AbstractHistory(this)
break
default:
assert(false, `invalid mode: ${mode}`)
}
}

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

this.app = app

const { mode, options, fallback } = this
switch (mode) {
switch (this.mode) {
case 'history':
this.history = new HTML5History(this, options.base)
this.history.transitionTo(getLocation(this.history.base))
break
case 'hash':
this.history = new HashHistory(this, options.base, fallback)
this.history.transitionTo(getHash(), () => {
window.addEventListener('hashchange', () => {
this.history.onHashChange()
Copy link
Member Author

@fnlctrl fnlctrl Oct 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what made flow unhappy..
Error log (when you remove the dummy method in the comment above):

src/index.js:75
 75:             this.history.onHashChange()
                              ^^^^^^^^^^^^ property `onHashChange`. Property not found in
 75:             this.history.onHashChange()
                 ^^^^^^^^^^^^ AbstractHistory

src/index.js:75
 75:             this.history.onHashChange()
                              ^^^^^^^^^^^^ property `onHashChange`. Property not found in
 75:             this.history.onHashChange()
                 ^^^^^^^^^^^^ HTML5History

Even when this.history is guaranteed to have onHashChange inside case 'hash'

})
})
break
case 'abstract':
this.history = new AbstractHistory(this)
break
default:
assert(false, `invalid mode: ${mode}`)
}

this.history.listen(route => {
Expand Down