-
Notifications
You must be signed in to change notification settings - Fork 2
/
GlobalMixin.js
59 lines (53 loc) · 1.3 KB
/
GlobalMixin.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
import Vue from 'vue'
import ReactiveProvide from './ReactiveProvide'
const {
provide: mergeProvide,
computed: mergeComputed,
inject: mergeInject,
} = Vue.config.optionMergeStrategies
export const createGlobalMixin = ({ name = 'reactiveProvide' } = {}) => ({
beforeCreate() {
let options = this.$options[name]
if (!options) return
if (typeof options === 'function') {
options = options.call(this, this)
}
/* istanbul ignore next */
if (typeof options !== 'object') return
const { beforeCreate, provide, inject, computed, watch } = ReactiveProvide(
options
)
// hook
beforeCreate.call(this, this)
//merging options
this.$options.computed = mergeComputed(
this.$options.computed,
computed,
this,
'computed'
)
this.$options.provide = mergeProvide(
this.$options.provide,
provide,
this,
'provide'
)
this.$options.inject = mergeInject(
this.$options.inject,
inject,
this,
'inject'
)
//applying the watch
this.$once('hook:created', () => {
const unwatch = this.$watch(
() => this[options.name],
watch[options.name].handler,
{
immediate: true,
}
)
this.$on('hook:beforeDestroy', unwatch)
})
},
})