如何为vuex实现带参数的 getter和state.commit
getter带参数
参考:https://vuex.vuejs.org/guide/getters.html#method-style-access
或者:https://stackoverflow.com/questions/37763828/javascript-es6-double-arrow-functions
官方例子:
getters:{ //... getTodoById:(state)=>(id)=>{ returnstate.todos.find(todo=>todo.id===id) } }
使用:
store.getters.getTodoById(2)//->{id:2,text:'...',done:false}
stackoverflow例子:
newVuex.Store({ getters:{ someMethod(state){ varself=this; returnfunction(args){ //returndatafromstorewithqueryonargsandselfasthis }; } } })
commit带参数
参考;https://stackoverflow.com/questions/46097687/vuex-passing-multiple-parameters-to-action和https://stackoverflow.com/questions/40522634/can-i-pass-parameters-in-computed-properties-in-vue-js
就是把第二个参数,以hash的形式传过来。
//vue页面调用: store.commit(INCREASE,{ vid:vid//这里可以容纳更多参数 }) //store.js constmutations={ [INCREASE](state,data){ pair=state.pairs.find((pair)=>{ returnpair.vid==data.vid//注意这里的data.vid就是了。 }) } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。