-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathindex.js
149 lines (125 loc) · 3.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* @flow */
import { install } from './install'
import { createMatcher } from './create-matcher'
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'
import { cleanPath } from './util/path'
import { normalizeLocation } from './util/location'
export default class VueRouter {
static install: () => void;
app: any;
options: RouterOptions;
mode: string;
history: HashHistory | HTML5History | AbstractHistory;
match: Matcher;
fallback: boolean;
beforeHooks: Array<?NavigationGuard>;
afterHooks: Array<?((to: Route, from: Route) => any)>;
constructor (options: RouterOptions = {}) {
this.app = null
this.options = options
this.beforeHooks = []
this.afterHooks = []
this.match = createMatcher(options.routes || [])
let mode = options.mode || 'hash'
this.fallback = mode === 'history' && !supportsHistory
if (this.fallback) {
mode = 'hash'
}
if (!inBrowser) {
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 {
return this.history && this.history.current
}
init (app: any /* Vue component instance */) {
assert(
install.installed,
`not installed. Make sure to call \`Vue.use(VueRouter)\` ` +
`before creating root instance.`
)
this.app = app
const history = this.history
if (history instanceof HTML5History) {
history.transitionTo(getLocation(history.base))
} else if (history instanceof HashHistory) {
history.transitionTo(getHash(), () => {
window.addEventListener('hashchange', () => {
history.onHashChange()
})
})
}
history.listen(route => {
this.app._route = route
})
}
beforeEach (fn: Function) {
this.beforeHooks.push(fn)
}
afterEach (fn: Function) {
this.afterHooks.push(fn)
}
push (location: RawLocation) {
this.history.push(location)
}
replace (location: RawLocation) {
this.history.replace(location)
}
go (n: number) {
this.history.go(n)
}
back () {
this.go(-1)
}
forward () {
this.go(1)
}
getMatchedComponents (): Array<any> {
if (!this.currentRoute) {
return []
}
return [].concat.apply([], this.currentRoute.matched.map(m => {
return Object.keys(m.components).map(key => {
return m.components[key]
})
}))
}
resolve (to: RawLocation, current?: Route, append?: boolean): {href: string} {
const normalizedTo = normalizeLocation(to, current || this.history.current, append)
const resolved = this.match(normalizedTo, current)
const fullPath = resolved.redirectedFrom || resolved.fullPath
const base = this.history.base
const href = createHref(base, fullPath, this.mode)
return {
normalizedTo,
resolved,
href
}
}
}
function createHref (base: string, fullPath: string, mode) {
var path = mode === 'hash' ? '/#' + fullPath : fullPath
return base ? cleanPath(base + path) : path
}
VueRouter.install = install
if (inBrowser && window.Vue) {
window.Vue.use(VueRouter)
}