Actions
jsx
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;
},
},
actions: {
changeNum(context, payload) {
// context 包含环境所处的上下文对象
setTimeout(() => {
context.commit("addNum", payload);
}, 1000);
},
},
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
vue
<template>
<div>
<p>{{ $store.state.num }}</p>
<button @click="asyncClick">异步按钮</button>
</div>
</template>
<script>
export default {
data() {
return {};
},
methods: {
asyncClick() {
// 调用actions中的方法
this.$store.dispatch("changeNum", 20);
},
},
};
</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