mutations
1.直接通过$store.commit调用
<button @click="$store.commit('mutations中的方法名','可带参数')">调用</button>
2.通过在methods中注册方法调用
import {mapMutations} from 'vuex'
export default {
data(){
return{
msg:"hello vuex"
}
},
methods:{
...mapMutations(['mutations中的方法名称'])
}
}
<button @click="mutations中的方法名称('可带参数')">-</button>
actions
1. 通过$store.dispatch 方法触发
add(){
this.$store.dispatch('actionAdd')
},
reduce(){
this.$store.dispatch('actionReduce')
}
add(){
this.$store.dispatch('actionAdd', {
num: 20
})
},
reduce(){
this.$store.dispatch(
{type:'actionReduce', num : 10}
)
}
2.通过在methods中注册方法调用
import { mapActions } from 'vuex';
export default {
data(){
return{
msg:"hello vuex"
}
},
methods:{
...mapActions(['actions中的方法名称']);
this.方法名('传参数')
}
}