Mutations
js
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
export default new Vuex.Store({
state: {
num: 10,
},
mutations: {
addNum(state, payload) {
state.num += payload;
},
},
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vue
<template>
<div>
<p>{{ $store.state.num }}</p>
<button @click="hdClick">同步按钮</button>
</div>
</template>
<script>
export default {
data() {
return {};
},
methods: {
hdClick() {
// 通过commit()调用mutations中的方法
this.$store.commit("addNum", 10);
},
},
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20