我是 ES7 的新手
我想在 Vue.js 中使用 async/await
这是我的代码
created (){
this.getA()
console.log(2)
this.getB()
},
methods : {
getA (){
console.log(1)
},
getB (){
console.log(3)
}
}
它返回
1
2
3
但是当我将它与 axios 一起使用时,
created (){
this.getA()
console.log(2)
this.getB()
},
methods : {
getA (){
$axios.post(`/getA`,params){
.then((result) => {
console.log(1)
})
},
getB (){
console.log(3)
}
}
它返回
2
3
1
所以我想在该代码中添加 async/await 。
如何使用异步/等待?
我试过了
async created (){
await this.getA()
console.log(2)
await this.getB()
},
methods : {
getA (){
$axios.post(`/getA`,params){
.then((result) => {
console.log(1)
})
},
getB (){
console.log(3)
}
}
它返回相同的结果。
原文由 USER 发布,翻译遵循 CC BY-SA 4.0 许可协议
您必须使用
then
或await
不能同时使用,如下所示:如果使用
then
如果使用
await
请注意,在调用 getB() 时,您不需要
then
或await
因为它不是异步的