vuex--action的使用

文章目录

场景
用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}
}

猜你喜欢

转载自blog.csdn.net/weixin_43755104/article/details/107620657