相关疑难解决方法(0)

如何在Vue.js中使用async / await?

我是ES7的新手

我想在Vue.js中使用async / await

这是我的代码

created (){
    this.getA()
    console.log(2)
    this.getB() 
},
methods : {
    getA (){
        console.log(1)
    },
    getB (){
        console.log(3)
    }
}
Run Code Online (Sandbox Code Playgroud)

它返回

1
2
3
Run Code Online (Sandbox Code Playgroud)

但是当我与axios一起使用时

created (){
    this.getA()
    console.log(2)
    this.getB() 
},
methods : {
    getA (){
        $axios.post(`/getA`,params){
        .then((result) => {
            console.log(1)
        })
    },
    getB (){
        console.log(3)
    }
}
Run Code Online (Sandbox Code Playgroud)

它返回

2
3
1
Run Code Online (Sandbox Code Playgroud)

所以我想在该代码中添加异步/等待。

如何使用异步/等待?

我试过了

async created (){
    await this.getA()
    console.log(2)
    await this.getB() 
},
methods : {
    getA (){
        $axios.post(`/getA`,params){
        .then((result) => {
            console.log(1)
        })
    },
    getB …
Run Code Online (Sandbox Code Playgroud)

javascript async-await vue.js ecmascript-2017

4
推荐指数
4
解决办法
9114
查看次数

标签 统计

async-await ×1

ecmascript-2017 ×1

javascript ×1

vue.js ×1