Vuex----Actions
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的更多相关文章
- [Vuex] Perform Async Updates using Vuex Actions with TypeScript
Mutations perform synchronous modifications to the state, but when it comes to make an asynchronous ...
- [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 ...
- [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. ...
- [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 ...
- vuex(1.0版本写法)
Vuex 是一个专门为 Vue.js 应用所设计的集中式状态管理架构. 官方文档:http://vuex.vuejs.org/zh-cn/ 2.0和1.0都能在此找到 每一个 Vuex 应用的核心就 ...
- vuex构建笔记本应用学习
vuex:针对vue应用派生的专门管理应用state的工具,state可以理解为我们组件需要操作的data数据,都知道,vue构建spa应用的时候,随着组件规模的提升,各个子组件之间的通信如果采用子组 ...
- Vue、Vuex+Cookie 实现自动登陆 。
概述 1.自动登陆实现思路. 2.vuex + cookie 多标签页状态保持. 自动登陆的需求: 1.登陆时勾选自动登陆,退出登陆或登陆到期后再次登陆后自动填写表单(记住密码)或访问登陆页自动登陆. ...
- 个人对vuex的表象理解(笔记)
一个东西,首先要知道为什么用它,为什么要vuex,官方解释为了解决繁杂事件订阅和广播,那么事件的$dispatch,$on,怎么就复杂了?许多人是不是感觉后者还挺简单的,对的 如果简单小型项目,那么不 ...
- [转] 对vuex的表象理解(笔记)
一个东西,首先要知道为什么用它,为什么要vuex,官方解释为了解决繁杂事件订阅和广播,那么事件的$dispatch,$on,怎么就复杂了?许多人是不是感觉后者还挺简单的,对的 如果简单小型项目,那么不 ...
- 【翻译】使用Vuex解决Vue中的身份验证
翻译原文链接:https://scotch.io/tutorials/handling-authentication-in-vue-using-vuex 我的翻译小站:https://www.zcfy ...
随机推荐
- windows2012安装.net4.7.2
第一步,下载.net4.7.2安装包 离线包:https://download.visualstudio.microsoft.com/download/pr/1f5af042-d0e4-4002-9c ...
- 山寨e网通公告
SHANZGONGG山寨e网通V1.0[换行]软件完全免费,官方绝不会索取任何费用,请勿被骗,后果自负.[换行]如果你有什么更好的建议或者需要哪里改进的地方,请联系作者QQ206044600反馈,前提 ...
- js中,作用域与作用域链的概念
1 作用域 声明的一个变量只在一段代码范围内是有效的,并不是总有效的.例如 : function father(){ // 声明变量 var val = "作用域内"; // 定义 ...
- 解决mysql使用sql文件不能还原数据库的问题
来源:https://bbs.sangfor.com.cn/forum.php?mod=viewthread&tid=109605 解决ERROR 1231 (42000): Variable ...
- mac Big Sur 安装MAT
1.下载MAT,https://www.eclipse.org/mat/previousReleases.php,这里安装最新版本1.12.0版本,这个依赖jdk11,需要安装openjdk11,请前 ...
- centos8.5安装kvm及kvm虚拟机的端口映射问题
1.安装KVM grep -Ei 'vmx|svm' /proc/cpuinfo|more #查看硬件是否支持虚拟化 yum install -y virt-* libvirt qemu-img ...
- SAP 弹出框 DEBUG
[FUNCTION] Command=/H Title=Debugger Type=SystemCommand 新建TXT存入代码,拖入弹出框
- vulnhub:My_Tomcat_Host靶机
kali:192.168.111.111 靶机:192.168.111.171 信息收集 端口扫描 nmap -A -v -sV -T5 -p- --script=http-enum 192.168. ...
- vue中标签的替换以及scoped实现css对当前文件起作用的原理
1,vue的工作原理其实就是我们前端拿到组件模板(也就是编译打包后生成的js文件,由vue动态生成html标签以及异步请求服务器的数据,更新html页面展示给用户) 如上图所示,public文件夹下的 ...
- 本地Map缓存
package com.cars.forwardservice.controller;import org.springframework.stereotype.Controller;import o ...