如何为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) => { return state.todos.find(todo => todo.id === id) } }
使用:
store.getters.getTodoById(2) // -> { id: 2, text: '...', done: false }
stackoverflow 例子:
new Vuex.Store({ getters: { someMethod(state){ var self = this; return function (args) { // return data from store with query on args and self as this }; } } })
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 const mutations = { [INCREASE](state, data){ pair = state.pairs.find( (pair) => { return pair.vid == data.vid // 注意这里的 data.vid 就是了。 }) } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
element-plus 在vue3 中不生效的原因解决方法(element-plus引入)
这篇文章主要介绍了element-plus 在vue3 中不生效的原因解决方法(element-plus引入),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-08-08解决ant Design中Select设置initialValue时的大坑
这篇文章主要介绍了解决ant Design中Select设置initialValue时的大坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-10-10Vue keepAlive实现不同的路由共用一个组件component的缓存问题(推荐)
这篇文章主要介绍了Vue keepAlive实现不同的路由共用一个组件component的缓存问题,实现方式使用Vue keepAlive实现页面缓存,需要的朋友可以参考下2022-08-08
最新评论