页面中定义两个方法
<button @click="add">+</button>
<button @click="sub">-</button>
<script>
export default {
methods: {
add(){
this.$store.commit('increment')
},
sub(){
this.$store.commit('decrement')
}
}
}
</script>
store中定义.
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
count: 1000
},
mutations: {
increment(state){
state.count++
},
decrement(state){
state.count--
}
},
actions: {},
modules: {}
})