1. 事例代码
(1)src/vuex/store.js中
// actions中传递参数 const mutations = { ADD (state, n) { state.count += n; } } // actions中传递参数 const actions ={ // 触发mutations中相应的方法 add ({commit}, n) { // 增加异步 setTimeout(()=>{ commit('ADD', n); },3000); console.log('我比reduce提前执行'); } }
(2)页面组件常规调用传递
// template <button @click="add">+</button> // script methods: { add() { // 分发action this.$store.dispatch('add', 99); } }
(3)页面组件使用mapActions调用传递
// template <button @click="add(99)">+</button> // script methods: { ...mapActions(['add']) }