[Vuex] Perform Async Updates using Vuex Actions with TypeScript
Mutations perform synchronous modifications to the state, but when it comes to make an asynchronous operation, they become useless.
Actions are a higher layer on the Vuex pattern, which allow to call mutations asynchronously or even call multiple mutations. This lesson guides you through using Vuex actions and type-safe them with TypeScript.
We want to add a todo by calling a API to get a todo from a server, then add into our todo app.
The idea is using 'Actions' to make Async request, then calling 'Mutations' with response data.
import {GetterTree, MutationTree} from 'vuex';
import {State} from '../types';
import {Todo} from '../types';
const state: State = {
todos: [
{text: 'Buy milk', checked: false},
{text: 'Learning', checked: true},
{text: 'Algorithom', checked: false},
],
};
export const getters: GetterTree<State, any> = {
todos: state => state.todos.filter(t => !t.checked),
dones: state => state.todos.filter(t => t.checked)
};
export const mutations: MutationTree<State> = {
addTodo(state, newTodo) {
const copy = {
...newTodo
};
state.todos.push(copy);
}
};
export const actions: ActionTree<tate, any> = {
addTodoAsync(context, id) {
fetch('https://jsonplaceholder.typicode.com/posts/'+id)
.then(data => data.json())
.then(item => {
const todo: Todo = {
checked: false,
text: item.title
}
context.commit('addTodo', todo);
})
}
}
export default state;
Add into root Store:
import Vue from 'vue';
import Vuex from 'vuex'; import todos, {getters, mutations, actions} from './todos'; Vue.use(Vuex); export default new Vuex.Store({
state: {
...todos,
},
getters,
mutations,
actions
});
Using '@Action' inside component:
<template>
<section>
<h4>Todos</h4>
<ul>
<li v-for="todo in todos">{{ todo.text }}</li>
</ul>
<h4>Done</h4>
<ul>
<li v-for="todo in dones">{{ todo.text }}</li>
</ul>
<p>
<label for="">
Add todo:
<input type="text" v-model="newTodo.text" @keyup.enter="addTodo(newTodo)">
</label>
<label for="">
Add todo async:
<input type="text" v-model="id" @keyup.enter="addTodoAsync(id)">
</label> </p>
</section>
</template> <script lang="ts">
import {Component, Vue} from 'vue-property-decorator';
import {Action, State, Getter, Mutation} from 'vuex-class';
import {Todo} from '../types'; @Component({
})
export default class Todos extends Vue {
@Getter todos: Todo[];
@Getter dones: Todo[]; @Mutation addTodo;
@Action addTodoAsync; newTodo: Todo = {
text: '',
checked: false
} id: string = '1';
}
</script>
[Vuex] Perform Async Updates using Vuex Actions with TypeScript的更多相关文章
- Vuex结合 async/await 优雅的管理接口请求
先看看 async/await 的语法 async 函数返回一个 Promise 对象 async 函数内部 return 返回的值.会成为 then 方法回调函数的参数. 1 2 3 4 async ...
- 详解Vuex常见问题、深入理解Vuex
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 状态?我把它理解为在data中的属性需要共 ...
- 浅读vuex源码,了解vuex基本原理
极简版vuex代码 class KVuex { constructor (options) { this.state = options.state this.mutations = options. ...
- vuex 源码:深入 vuex 之辅助函数 mapState
前言 当一个组件要获取多个 state 的时候,声明计算属性就会变得重复和冗余了.我们可以使用到辅助函数 mapState 来更快更简洁地生成计算属性. 所以我们得清楚,mapState 的作用就是帮 ...
- [Vue] Use Vue.js Watchers to Respond to Async Updates
Use watchers to keep an eye on your data. Watchers are methods that are invoked when the specified a ...
- [Vue + TS] Watch for Changes in Vue Using the @Watch Decorator with TypeScript
Vue watchers allow to perform async updates as a side effect of a property change. This lesson shows ...
- vuex所有核心概念完整解析State Getters Mutations Actions
vuex是解决vue组件和组件件相互通信而存在的,vue理解起来稍微复杂但一旦看懂择即为好用 安装: npm install --save vuex 引入 import Vuex from 'vuex ...
- vuex 笔记
Vuex 笔记 一个简单的状态管理 单一数据源: const sourceOfTruth = {} const vmA = new Vue({ data: sourceOfTruth }) const ...
- vuex 使用文档
安装 直接下载CDN 引用 <script src="/path/to/vue.js"></script> <script src="/pa ...
随机推荐
- 解析eBay BASE模式、去哪儿及蘑菇街分布式架构
目录:问题分析概念解读Most Simple原理解读eBey.去哪儿.蘑菇街分布式事务案例分析 参考资料 1.问题解析 要想做架构,必须识别出问题,即是谁的问题,什么问题.明显的,分布式架构解决 ...
- L3-021 神坛 (30 分) 计算几何
在古老的迈瑞城,巍然屹立着 n 块神石.长老们商议,选取 3 块神石围成一个神坛.因为神坛的能量强度与它的面积成反比,因此神坛的面积越小越好.特殊地,如果有两块神石坐标相同,或者三块神石共线,神坛的面 ...
- Kafka、RabbitMQ、RocketMQ等消息中间件的对比 —— 消息发送性能和区别
https://blog.csdn.net/yunfeng482/article/details/72856762
- UVA 508 Morse Mismatches JAVA
题意:输入字母和数字的编码,输入词典,输入一段编码,求出对应的单词. 思路:来自https://blog.csdn.net/qq_41163933/article/details/82224703 i ...
- sql的简单操作
mysql 一.mysql简介和安装 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应 ...
- BZOJ.5285.[AHOI/HNOI2018]寻宝游戏(思路 按位计算 基数排序..)
BZOJ LOJ 洛谷 话说vae去年的专辑就叫寻宝游戏诶 只有我去搜Mystery Hunt和infinite corridor了吗... 同样按位考虑,假设\(m=1\). 我们要在一堆\(01\ ...
- 2017-9-24-Linux移植:ubuntu server 16.04无法联网&无法apt-get update解决
无法上网!!!不能忍.. 现象:ifconfig 毛都没有,想找一下ip都找不到. ifconfig –a 可以列出所有网卡设备,确认VM VirtualBox网卡开对了,已经给到了虚拟机. 编辑/e ...
- 英语口语练习系列-C26-广告-人际关系-辨别物体-如果
词汇-广告 advertisement noun [ C ] UK /ədˈvɜː.tɪs.mənt/ US /æd.vɝːˈtaɪz.mənt/ informal ad, uk also infor ...
- JavaScript基础笔记(三) 引用类型
引用类型 引用类型的值(对象)是引用类型的一个实例. 一.Object类型 创建Object实例: //方法一:通过new操作符创建 var a = new Object(); a.neme = &q ...
- go 单元测试时读取配置文件
在go项目中读取配置文件时,如果使用的是相对路径,在执行run test时也会在test文件所在的目录下去读取配置文件,如果文件没在此目录下会报错:“open env1.json: The syste ...