UNPKG

1.4 kBJavaScriptView Raw
1import View from './components/view'
2import Link from './components/link'
3
4export let _Vue
5
6export function install (Vue) {
7 if (install.installed && _Vue === Vue) return
8 install.installed = true
9
10 _Vue = Vue
11
12 const isDef = v => v !== undefined
13
14 const registerInstance = (vm, callVal) => {
15 let i = vm.$options._parentVnode
16 if (isDef(i) && isDef(i = i.data) && isDef(i = i.registerRouteInstance)) {
17 i(vm, callVal)
18 }
19 }
20
21 Vue.mixin({
22 beforeCreate () {
23 if (isDef(this.$options.router)) {
24 this._routerRoot = this
25 this._router = this.$options.router
26 this._router.init(this)
27 Vue.util.defineReactive(this, '_route', this._router.history.current)
28 } else {
29 this._routerRoot = (this.$parent && this.$parent._routerRoot) || this
30 }
31 registerInstance(this, this)
32 },
33 destroyed () {
34 registerInstance(this)
35 }
36 })
37
38 Object.defineProperty(Vue.prototype, '$router', {
39 get () { return this._routerRoot._router }
40 })
41
42 Object.defineProperty(Vue.prototype, '$route', {
43 get () { return this._routerRoot._route }
44 })
45
46 Vue.component('RouterView', View)
47 Vue.component('RouterLink', Link)
48
49 const strats = Vue.config.optionMergeStrategies
50 // use the same hook merging strategy for route hooks
51 strats.beforeRouteEnter = strats.beforeRouteLeave = strats.beforeRouteUpdate = strats.created
52}