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. xorg 屏幕分辨率设置(x11分辨率设置/linux分辨率设置)

    记录一下,用于linux虚拟机分辨率设置.https://blog.csdn.net/weixin_36084095/article/details/116839103(在谷歌搜索是简书的文章,在百度 ...

  2. debian11 bspwm+polybar问题记录(siji字体无法正常显示)

    一.siji字体无法显示. 很懒很菜,就想用开箱即用的原始配置依然遇到了问题...plybar中的bitmap字体siji无法正常显示.即便按照github的siji官方脚本安装了siji字体还是不行 ...

  3. 从零搭建hadoop集群之zookeeper集群安装

    1. 从官方渠道获取对应的zookeeper的安装包 http://archive.apache.org/dist/zookeeper/ zookeeper-3.4.10.tar.g 2. 上传zoo ...

  4. AndroidStudio 集成kotlin,以及Kotlin-gradle-plugin-1.5.0.jar 下载失败

    配置Kotlin buildscript { ext.kotlin_version = '1.5.0' repositories { maven{url 'http://maven.aliyun.co ...

  5. 玩玩 Visual Studio Code 和 MSYS2

    注意:为了便于理解本文内容,您可能需要一些前置知识,例如命令行操作,编译器操作,路径操作,环境变量操作,vscode操作-- 众所周知 Visual Studio Code 是一个优秀的编辑器. 众所 ...

  6. AVL tree rotate

    AVL tree single rotate /** * Rotate binary tree node with left child. * For AVL trees, this is a sin ...

  7. 原创分享 HubbleDotNet 最新绿色版,服务端免安装,基于eaglet 最后V1.2.8.9版本开发,bug修正,支持一键生成同步表

    HubbleDotNet 是一个基于.net framework 的开源免费的全文搜索数据库组件.开源协议是 Apache 2.0.HubbleDotNet提供了基于SQL的全文检索接口,使用者只需会 ...

  8. sqlalchemy 数据类型

  9. 前端项目线上部署记录 | vue-cli

    一.修改公开路径后打包;npm run build 新建一个vue.config.js文件,如果本地打开,则路径为"./',线上则'/',不加'.' module.exports = { p ...

  10. uniapp 图片文件流

    uni.request({ url: '*****', //仅为示例,并非真实接口地址. method: 'GET', responseType: 'arraybuffer', data: {}, h ...