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)
})
})
}
}
}

最新文章

  1. esri联邦用户大会 总结
  2. git中的版本库,暂存区和工作区
  3. [LINUX] 查看连接数和IO负载
  4. 转载--thinkphp框架的路径问题 - 总结
  5. C#强制清除缓存
  6. jsp或Action获取请求参数中文乱码
  7. wsse:InvalidSecurity Error When Testing FND_PROFILE Web Service in Oracle Applications R 12.1.2 from SOAP UI (Doc ID 1314946.1)
  8. CAS SSO:汇集配置过程中的错误解决方法
  9. js中的潜伏者之Arguments对象
  10. VS2010无法断点调试解决办法
  11. 关于HTML5新手应该知道的几点知识
  12. Rsync(远程同步): linux中Rsync命令的实际示例
  13. New UWP Community Toolkit - Staggered panel
  14. Java的类加载器种类(双亲委派)
  15. phpcms中set_config和get_sysinfo函数
  16. ssh防止暴力破解之fail2ban
  17. MyBatis自动生成Java/C#的Bean(Entity)的等价MYSQL实现函数
  18. 命名实体识别,使用pyltp提取文本中的地址
  19. Maven 添加jar包到本地仓库
  20. verilog语法实例学习(1)

热门文章

  1. The stacking context
  2. c#所有部门及其下所部门生成树形图(递归算法获取或键值对方式获取)
  3. EasyUI报错 $(...).accordion is not a function
  4. 一文读懂遗传算法工作原理(附Python实现)
  5. 自定义mysql类用于快速执行数据库查询以及将查询结果转为json文件
  6. 【PAT】B1043 输出PATest(20 分)
  7. shell脚本之数组
  8. DataUtils对Connection的获取、释放和关闭的操作学习
  9. 基础数据类型之AbstractStringBuilder
  10. JS实现奇偶数的判断