一、vue中使用async和await解决异步
转载自知乎:(https://zhuanlan.zhihu.com/p/72817372)
async的解释:
async的用法,作为一个关键字放到函数之前,表示函数是异步的函数,因为async就是异步的意思, 异步函数也就意味着该函数的执行不会阻塞后面代码的执行,async 函数返回的是一个promise 对象
await的解释:
await的含义为等待。意思就是代码需要等待await后面的函数运行完并且有了返回结果之后,才继续执行下面的代码,同步的效果
methods:{
async getDeleteComment () {
let params = {
name: this.topic,
subtopic: this.subtopic,
thirdtopic: this.thirdtopic,
except: this.except,
}
const resp = await Api.delete_comment_removedpost(params)
if (resp.code == 0 && !_.isEmpty(resp.data.list)) { //判断list是否为空
const {data:{list,count}} = resp; //解构赋值的一种写法
this.list = list; //要渲染的数组
this.count_num = count //分页
}else{
this.list = [];
}
}
mounted() {
this.getDeleteComment()
}
}
网友评论