mutations修改状态

首先说一下传值

1
2
3
4
5
6
7
8
const mutations = {
add(state,num){
state.count+=num
},
reduce(state,num){
state.count-=num
}
}
1
2
<button @click="$store.commit('add',2)">+</button>
<button @click="$store.commit('reduce',3)">-</button>

然后跟上篇一样在调用的时候写法太烦,类似的我们用mapMutations

1
2
 import {mapState,mapMutations} from'vuex'
methods:mapMutations(['add','reduce']),
1
2
<button @click="add(1)">+</button>
<button @click="reduce(1)">-</button>
-------------本文结束感谢您的阅读-------------