I am very interested in this little implementation but can't get it to work.
I maybe did something wrong...
here is my modified implementation:
classStore{constructor(options={}){this._state={...options.state}this._actions={...options.actions}this._mutations={...options.mutations}}dispatch(action,payload){if(!this._actions[action]){thrownewError(`[Error] action ${action} is undefined`)}returnthis._actions[action]({commit:this.commit.bind(this),state:{...this._state}},payload)}commit(type,payload){if(!this._mutations[type]){thrownewError(`[Error] mutation ${type} is undefined`)}returnthis._mutations[type].call(null,this._state,payload)}}functioninstall(Vue){Vue.$store=Vue.mixin({created(){if(this.$options.store){this.$store=this.$options.store}elseif(this.$options.parent&&this.$options.parent.$store){this.$store=this.$options.parent.$storeVue.util.defineReactive(this,'state',this.$store)}}})}exportdefault{install,Store}
I can dispatch the actions, commit the mutations and I can see my state has been updated but I don't understant how to get the state value in my vue file :(
For example after mutating a users array in the state, I try to use this :
Hi!
I am very interested in this little implementation but can't get it to work.
I maybe did something wrong...
here is my modified implementation:
I can dispatch the actions, commit the mutations and I can see my state has been updated but I don't understant how to get the state value in my vue file :(
For example after mutating a users array in the state, I try to use this :
this.$store.state.users
this.$store.users
this.state.users
but neither of these are working...
Could you tell me what I'm doing wrong ?
Thanks!
What do you get when you
console.log(Vue.$store)
?I just modified this line and it worked
Vue.util.defineReactive(this, 'state', this.$store._state)
Nice to know that it worked.