vue 中使用 async/await 将 axios 异步请求同步化处理
1. axios 常规用法:
export default {
name: 'Historys',
data() {
return {
totalData: 0,
tableData: []
}
},
created () {
this.getHistoryData()
},
methods: {
handleClick (tab) {
let data = {
status: tab.name,
name: this.formInline.user,
cid: this.formInline.identity,
start_time: this.formInline.dateTime ? this.formInline.dateTime[0] : '',
end_time: this.formInline.dateTime ? this.formInline.dateTime[1] : ''
}
this.getHistoryData()
},
// 统一处理axios请求
getHistoryData (data) {
axios.get('/api/survey/list/', {
params: data
}).then((res) => {
console.log(res)
this.tableData = res.data.result
this.totalData = res.data.count
}).catch((err) => {
console.log(err)
alert('请求出错!')
})
}
}
}
2. 使用 asyns/await 将 axios 异步请求同步化:
2.1 当 axios 请求拿到的数据在不同场景下做相同的处理时:
export default {
name: 'Historys',
data() {
return {
totalData: 0,
tableData: []
}
},
created () {
this.getHistoryData()
},
methods: {
handleClick (tab) {
let data = {
status: tab.name,
name: this.formInline.user,
cid: this.formInline.identity,
start_time: this.formInline.dateTime ? this.formInline.dateTime[0] : '',
end_time: this.formInline.dateTime ? this.formInline.dateTime[1] : ''
}
this.getHistoryData()
},
// 统一处理axios请求
async getHistoryData (data) {
try {
let res = await axios.get('/api/survey/list/', {
params: data
})
this.tableData = res.data.result
this.totalData = res.data.count
} catch (err) {
console.log(err)
alert('请求出错!')
}
}
}
}
2.2 当 axios 请求拿到的数据在不同场景下做不同的处理时:
export default {
name: 'Historys',
data() {
return {
totalData: 0,
tableData: []
}
},
async created () {
try {
let res = await this.getHistoryData()
console.log(res)
// 等拿到返回数据res后再进行处理
this.tableData = res.data.result
this.totalData = res.data.count
} catch (err) {
console.log(err)
alert('请求出错')
}
},
methods: {
async handleClick (tab) {
let data = {
status: tab.name,
name: this.formInline.user,
cid: this.formInline.identity,
start_time: this.formInline.dateTime ? this.formInline.dateTime[0] : '',
end_time: this.formInline.dateTime ? this.formInline.dateTime[1] : ''
}
try {
let res = await this.getHistoryData()
console.log(res)
// 等拿到返回数据res后再进行处理
this.tableData = res.data.result
this.totalData = res.data.count
} catch (err) {
console.log(err)
alert('请求出错')
}
},
// 封装axios请求,返回promise, 用于调用getHistoryData函数后作不同处理
getHistoryData (data) {
return new Promise((resolve, reject) => {
axios.get('/api/survey/list/', {
params: data
}).then((res) => {
resolve(res)
}).catch((err) => {
reject(err)
})
})
}
}
}
vue 中使用 async/await 将 axios 异步请求同步化处理的更多相关文章
- [C#] .NET4.0中使用4.5中的 async/await 功能实现异步
在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framework 4.0中却无法使用.这时不免面临着抉择,到底是升级整个解决方案还是不使用呢? 如果你的软件还没发布出去 ...
- .NET4.0中使用4.5中的 async/await 功能实现异步
在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framework 4.0中却无法使用.这时不免面临着抉择,到底是升级整个解决方案还是不使用呢? 如果你的软件还没发布出去 ...
- 在vue中使用async/await遇到的坑
最近无聊在搞一些新的东西,今天就遇到一个async/await的坑: 因为我用的不是vue官方的脚手架,所以遇到这样的问题: await is a reserved word 这样的警告,我猜应该是缺 ...
- 【vue】---vue中使用async+await出现的问题及解决方案
一.在Vue中出现的问题 因为我没有用脚手架,自己用webpack配置的环境,因此报了以下错误,出现的问题应该是缺少解析器的原因 二.解决方案 安装: npm i babel-plugin-trans ...
- vue中用 async/await 来处理异步
原文作者:https://www.cnblogs.com/SamWeb/p/8417940.html 昨天看了一篇vue的教程,作者用async/ await来发送异步请求,从服务端获取数据,代码很简 ...
- 用 async/await 来处理异步
昨天看了一篇vue的教程,作者用async/ await来发送异步请求,从服务端获取数据,代码很简洁,同时async/await 已经被标准化,是时候学习一下了. 先说一下async的用法,它作为一个 ...
- 用async/ await来发送异步
昨天看了一篇vue的教程,作者用async/ await来发送异步请求,从服务端获取数据,代码很简洁,同时async/await 已经被标准化,是时候学习一下了. 先说一下async的用法,它作为一个 ...
- 【转】用 async/await 来处理异步
原文地址:https://www.cnblogs.com/SamWeb/p/8417940.html 昨天看了一篇vue的教程,作者用async/ await来发送异步请求,从服务端获取数据,代码很简 ...
- 用 async/await 来处理异步(转)
昨天看了一篇vue的教程,作者用async/ await来发送异步请求,从服务端获取数据,代码很简洁,同时async/await 已经被标准化,是时候学习一下了. 先说一下async的用法,它作为一个 ...
随机推荐
- python第二十一天---昨天没写完作业
作业 2, 模拟计算器开发:实现加减乘除及拓号优先级解析用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 ...
- Servlet (HttpServletResponse)对象
1.setStatus(int status)方法:用于设置HTTP响应消息的状态码,并生成响应状态行.响应状态行中的状态描述信息直接与状态码相关,HTTP版本由服务器确定,因此只需要通过这个方法设置 ...
- 【PAT】B1057 数零壹(20 分)
简单题,简单字符串处理加简单数学进制转换 #include<stdio.h> #include<string.h> #include<ctype.h> int ma ...
- Java设计模式之七 ----- 享元模式和代理模式
前言 在上一篇中我们学习了结构型模式的组合模式和过滤器模式.本篇则来学习下结构型模式最后的两个模式, 享元模式和代理模式. 享元模式 简介 享元模式主要用于减少创建对象的数量,以减少内存占用和提高性能 ...
- SALALchemy Session与scoped_session的源码分析
我们发现Session与scoped_session都有一些方法: 但是scoped_session的源码里面没有设置这些方法让我们从源码里去窥探下源码在哪里设置了这些方法: Session里面的方法 ...
- Java中关于CountDownLatch的使用
CyclicBarrier工具类主要是控制多个线程的一起执行,演示程序: import java.util.Random; import java.util.concurrent.CountDownL ...
- 有时间研究一下Spark的HashPartitioner和RangePartitioner
有时间研究一下Spark的HashPartitioner和RangePartitioner有时间研究一下Spark的HashPartitioner和RangePartitioner有时间研究一下Spa ...
- (2)HomeAssistant 参数配置
如果你希望使用其它目录作为配置文件所在地,可以使用以下命令启动home assistant:hass --config path/to/config 在配置目录下,有一个文件configuration ...
- Click to add to Favorites Troubleshooting: High Version Count Issues (Doc ID 296377.1)
Copyright (c) 2018, Oracle. All rights reserved. Oracle Confidential. Click to add to Favorites Trou ...
- oracle ORA-20011: Approximate NDV failed: ORA-29913: error in executing ODCIEXTTABLEOPEN callout
Mon Jun 11 21:59:52 2018LNS: Standby redo logfile selected for thread 2 sequence 132997 for destinat ...