let Vue;
class Store{
constructor(options){
this.vm = new Vue({
date: {
state: options.state
}
});
this.getters={};
Object.keys(options.getters).forEach(getterName=>{
Object.definePropery(this.getters, getterame,{
get:()=>{return options.getters[getterName](this.state)}
})
});
this.mutations = {};
Object.keys(options.mutations).forEach(mutationsName=>{
this.mutations[mutationsName] = (payload)=>{
options.mutations[mutationsName](this.state, payload)
}
});
this.actions = {};
Object.keys(options.actions).forEach(actionsName=>{
this.actions[actionsName] = (payload)=>{
options.actions[actionsName](this, payload)
}
});
}
get state(){
return this.vm.state
}
commit = (mutationName, payload) => {this.mutations[mutationsName](payload)};
dispatch = (actionName, payload) =>{this.actions[actionsName](payload)}
}
const install = (_Vue) => {
Vue = _Vue;
Vue.mixin({
beforeCreate(){
if(this.$options.store){
this.$store = this.$options.store
}else{
this.$store = this.$parent && this.$parent.$store
}
}
})
}
export default{
Store,
install
}