Vue解决axios接口异步请求数据不同步返回的问题-async/await

417 阅读1分钟

​​axios​​可以通过​​async​​和​​await​​来实现同步请求。

​​async​​是用来修饰方法的,声明该方法为异步请求方法;

​​await​​只能在​​async​​修饰的方法中使用,它的功能和同步请求的功能是一样,当​​await​​修饰一行代码的时候,前端的加载走到这行代码的时候就会变成同步加载,只有等到这行代码执行完成之后才会执行后面的代码。

``

```async get(cyhb) {
      await axios
        .get("https://api.exchangerate-api.com/v4/latest/" + cyhb)
        .then((res) => {
          this.getdata = res.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    
    
//2.    
async cxjs_btn() { 
      await this.get(this.cyhb_sx);
    },