Vue中axios异步请求的同步化Demo
错误写法Demo:没有将await返回的结果继续return
//用async进行异步函数声明,在需要同步化的函数调用前添加await关键字
async fun() {
const res = await this.req(url,params)
console.log("请求响应结果->", res)
},
async req(url,params) {
//此处应将then或者cathc中return的结果再作为axios的结果进行返回,如果没有返回,则调用req的函数接受不到返回值,打印结果为undefinded
//正确写法对应Demo-v1或者Demo-v2
await axios({
method: "post",
url: url
params: params
}).then(res => {
return res.data
}).catch(err => {
console.<

最低0.47元/天 解锁文章
1058

被折叠的 条评论
为什么被折叠?



