-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[vue] 如果将axios异步请求同步化处理? #328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
async ,await |
|
async ,await |
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('请求出错!')
}
}
} 同步的axios.get调用返回的是一个状态为pending的promise,等异步请求有了结果,这个promise的状态就会settled,要么成功要么失败了 异步有了结果后,res就能拿到成功的response,没有结果的时候 这种async await的写法,只是写法上同步化了 |
使用async/await结合Promise来实现一种类似同步的代码风格,使代码看起来更加同步化。 定义一个异步函数:在合适的地方(如Vue组件的方法中)定义一个异步函数,用于执行异步操作。
调用异步函数:在需要执行异步请求的地方,调用定义的异步函数。
通过使用async/await,你可以在异步请求的地方使用await关键字等待异步操作的完成,并根据结果进行后续处理。但请注意,这仅是一种语法糖,实际上仍然是异步执行的,不会阻塞JavaScript的执行。 |
[vue] 如果将axios异步请求同步化处理?
The text was updated successfully, but these errors were encountered: