场景
用action,一般是异步调用,延迟调用一个方法
方法1
使用方法
在调用的组件中使用
例如
methods : {
handle() {
this.$store.dispatch('addNAsync',5)
}}
在store.js中的actions中写
actions: {
addNAsync{context,step) {
setTimeout({}=> {
context.commit('addN', step)},1000)
}
方法2导入法
在要用到的组件中
import { mapState,mapMutatios,mapActions } from 'vuex'
然后在
methods: {
...mapAction(['subAsync'])
btnHandler3() {
this.subAsync()
}
在store.js中写
subAsync(context) {
setTimeout(() => {
context.commit('sub'),1000}
}