[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 ...
随机推荐
- appium---第一个脚本--启动一个已存在的app
1.可以使用android-sdk中的aapt工具 ①.选择一个版本的build_tools,加入path环境变量中 ②.验证aapt环境是否正常 3.下载你要测试的包,放入某一地址中(随意):aap ...
- windows docker常用命令
关键词 示例 作用 attach sudo docker run -itd ubuntu:14.04 /bin/bash 进入容器 exec docker exec -it mysql bash 在容 ...
- Codeforces Round #460 (Div. 2) ABCDE题解
原文链接http://www.cnblogs.com/zhouzhendong/p/8397685.html 2018-02-01 $A$ 题意概括 你要买$m$斤水果,现在有$n$个超市让你选择. ...
- spring框架等web程序在tomcat下的启动顺序
http://www.cnblogs.com/panxuejun/p/5847774.html
- Ubuntu16.04+Opencv3.3的安装教程
需要准备的基本材料(请先看完整个安装过程再进行下面的操作): 一.到Opecv-Release的Github项目上下载最新的Opencv版本,注意---基于python2.7,可选用 OpenCV2. ...
- POJ 2488 A Knight's Journey-dfs
题目链接:http://poj.org/problem?id=2488 题目解读:首先得弄清楚国际象棋中关于“马走日”的规则,如上图中的马,它的下一步的走法有8中,所以对每一个位置的马,它所能走的8个 ...
- JAVA首次课堂测试总结
暑期生活已经结束,新的学期也已经开始,而暑期放假之前约定的JAVA首次课堂测试也如期的到来,本次测试真的可以学到和多东西,也有很多感想. 首先体会最深的就是系主任所说的软件工程不是那么好学的,真的需要 ...
- Python 爬虫利器 Selenium 介绍
Python 爬虫利器 Selenium 介绍 转 https://mp.weixin.qq.com/s/YJGjZkUejEos_yJ1ukp5kw 前面几节,我们学习了用 requests 构造页 ...
- Nowcoder contest 370B Rinne Loves Graph 【分层图最短路】
<题目链接> 题目大意: Island 是有一些奇怪的城镇和道路构成的(题目需要,游戏党勿喷),有些城镇之间用双向道路连接起来了,且每条道路有它自己的距离.但是有一些城镇已经被派兵戒严,虽 ...
- Python自制微信机器人:群发消息、自动接收好友
运营公众号也有半年了,今年5月份开始的,之前一直用一款windows工具来运营自动接受好友请求.群发文章.自动回复等操作,但颇有不便. 举几个场景: 突然在外面看到一篇文章很好,临时写了一篇,想群发一 ...