## 修改数据(异步操作)
# 不能直接 修改 state 只能通过 mutations 修改
~~~
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
cont: 0
},
mutations: {
add (state) {
state.cont++
},
addn (state, num: number) {
state.cont += num
},
jian (state, num:number) {
state.cont -= num
}
},
actions: {
addAsync (context) {
// 定时器
setTimeout(() => {
context.commit('add')
}, 1000)
}
},
modules: {}
})
~~~
## 异步修改第一种
~~~
this.$store.dispatch('addAsync',参数)
~~~
# 异步修改第二种
1.
import { mapState, mapActions } from 'vuex'
2.
this.addAsync()
3
...mapActions(['addAsync']),