Actions用于处理异步任务。

如果通过异步操作变更数据,必须通过 Action,而不能使用Mutation,但是在 Action中还是要通过触发Mutation的方式间接变更数据。

注意: 在Actions 中不能直接修改 state中的数据,要通过 mutations修改。

方法1:this.$store.dispatch

const store = new Vuex.store({
state: {
count: 0
},
mutations: {
add(state) {
state.count++
}
},
actions: {
addAsync(context) {
setTimeout(() => {
context.commit('add')
}, 1000);
}
},
})

  

methods:{
handle(){
this.$store.dispatch('addAsync')
}
}

方法2:导入 mapActions 函数

import {mapActions} from 'vuex'
actions: {
subAsync(context){
setTimeout(() => {
context.commit('sub')
}, 1000);
}
}
methods:{
...mapActions(['subAsync']),
decrementAsync(){
this.subAsync()
}
}

  

Vuex----Actions的更多相关文章

  1. [Vuex] Perform Async Updates using Vuex Actions with TypeScript

    Mutations perform synchronous modifications to the state, but when it comes to make an asynchronous ...

  2. [Nuxt] Update State with Vuex Actions in Nuxt.js

    You can conditionally add classes to Vue.js templates using v-bind:class. This will help display the ...

  3. [Nuxt] Use Vuex Actions to Delete Data from APIs in Nuxt and Vue.js

    You'll begin to notice as you build out your actions in Vuex, many of them will look quite similar. ...

  4. [Nuxt] Build a Vue.js Form then use Vuex Actions to Post to an API in Nuxt

    The default behavior of submitting an HTML form is to reload the page. You can use the Vue.js @submi ...

  5. vuex(1.0版本写法)

    Vuex 是一个专门为 Vue.js 应用所设计的集中式状态管理架构. 官方文档:http://vuex.vuejs.org/zh-cn/  2.0和1.0都能在此找到 每一个 Vuex 应用的核心就 ...

  6. vuex构建笔记本应用学习

    vuex:针对vue应用派生的专门管理应用state的工具,state可以理解为我们组件需要操作的data数据,都知道,vue构建spa应用的时候,随着组件规模的提升,各个子组件之间的通信如果采用子组 ...

  7. Vue、Vuex+Cookie 实现自动登陆 。

    概述 1.自动登陆实现思路. 2.vuex + cookie 多标签页状态保持. 自动登陆的需求: 1.登陆时勾选自动登陆,退出登陆或登陆到期后再次登陆后自动填写表单(记住密码)或访问登陆页自动登陆. ...

  8. 个人对vuex的表象理解(笔记)

    一个东西,首先要知道为什么用它,为什么要vuex,官方解释为了解决繁杂事件订阅和广播,那么事件的$dispatch,$on,怎么就复杂了?许多人是不是感觉后者还挺简单的,对的 如果简单小型项目,那么不 ...

  9. [转] 对vuex的表象理解(笔记)

    一个东西,首先要知道为什么用它,为什么要vuex,官方解释为了解决繁杂事件订阅和广播,那么事件的$dispatch,$on,怎么就复杂了?许多人是不是感觉后者还挺简单的,对的 如果简单小型项目,那么不 ...

  10. 【翻译】使用Vuex解决Vue中的身份验证

    翻译原文链接:https://scotch.io/tutorials/handling-authentication-in-vue-using-vuex 我的翻译小站:https://www.zcfy ...

随机推荐

  1. uniapp开发的app打开微信小程序

    第一种 <script> export default { data() { return { sweixin: null } }, onLoad() { this.getPlus() } ...

  2. uniapp 弹窗输入

    借鉴链接:https://blog.csdn.net/qq_40894259/article/details/110200721 <template> <view class=&qu ...

  3. 虚拟机中Linux分区扩容

    打开Virtualbox所在的安装目录,执行以下命令,命令中的虚拟有磁盘路径改成自己的: 调整容量前,先关闭虚拟机.接着,打开CMD,进入VirtualBox的安装目录,执行VBoxManage li ...

  4. ubuntu22.04LTS下编译glfw

    环境准备 # 预装cmake sudo apt install cmake # 下载源码 git clone https://github.com/glfw/glfw.git 编译 # cmake初始 ...

  5. csec的key更新

    在对csec的使用中(其他遵循hsm key update协议的芯片也适用),kdf的运算过程中遇到的数据都是128bit.不需要考虑padding的问题.目前并没有找到对padding的一致性的处理 ...

  6. Excel 的盒须图 离群值 Outliers

    Excel 中的盒须图 翻译自https://www.excel-easy.com/examples/box-whisker-plot.html 本示例教您如何在Excel中创建盒须图.盒须图显示了数 ...

  7. 手机安装python环境

    一.安装Termux环境 1.下载Termux Qpython 安装以后玩爬虫各种报错,也就不纠结了,直接弄Termux 虚拟环境 下载链接:https://wiki.termux.com/wiki/ ...

  8. Finance财务软件(支持Excel模板打印专题)

    我们可以修改模板文件./service/PrintTemplate/凭证打印模板_v1.xlsx 模板中的字段对应 2010_upgrade_01.sql 中的存储过程sp_voucher_print ...

  9. RestTemplate 连接池最大链接数

    原文链接:https://www.cnblogs.com/x-x-736880382/p/11591906.html 以前我们项目都是基于Apache HttpClient 连接池进行web 接口调用 ...

  10. Visual Studio常用的宏

    $(SolutionDir) 表示获取解决方案文件.sln所在文件夹 $(ProjectDir 获取项目工程文件.vcxproj所在文件夹 $(Configuration) 获取编译后的Debug / ...