let Vue;
const install = (_Vue) => {
// 先判断是否use过
if (Vue && _Vue === Vue) {
console.error('[vuex] already installed. Vue.use(Vuex) should be called only once.')
return
}
// 绑定Vue的构造函数
Vue = _Vue
// 判断Vue版本,2版本推荐用mixin
// 1版本单例,放到原型上(会影响其他Vue实例)
const version = Number(Vue.version.split('.')[0])
if (version >= 2) {
Vue.mixin({
beforeCreate() {
const options = this.$options
// store injection
if (options.store) {
this.$store = typeof options.store === 'function'
? options.store()
: options.store
} else if (options.parent && options.parent.$store) {
this.$store = options.parent.$store
}
}
})
} else {
// override init and inject vuex init procedure
// for 1.x backwards compatibility.
const _init = Vue.prototype._init
Vue.prototype._init = function (options = {}) {
options.init = options.init
? [vuexInit].concat(options.init)
: vuexInit
_init.call(this, options)
}
}
}
class Store {
construtor(options = {}) {
this._vm = new Vue({
data: {
stage: options.state,
}
})
let getters = options.getters;
this.getters = {}
Object.keys(getters).forEach(getterName => {
Object.defineProperty(this.getters, getterName, {
get: () => getters[getterName](this.state)
enumerable: true, // for local getters
})
})
}
get state () {
return this._vm._data.$$state
}
set state (v) {
if (__DEV__) {
assert(false, `use store.replaceState() to explicit replace store state.`)
}
}
commit = (type, payload, options) => {
}
}
export default {
Store,
install,
}