【发布时间】:2019-07-24 02:58:00
【问题描述】:
我是 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)
}
}
返回相同的结果。
【问题讨论】:
-
getA不返回承诺。
标签: javascript vue.js async-await ecmascript-2017