es7如何使用await發送請求


handleLogin() {
      this.$http.post("login", this.formLabelAlign).then(res => {
        const {
          data,
          meta: { msg, status }
        } = res.data;
        if (status === 200) {
          this.$message({
            showClose: true,
            message: msg,
            type: "success"
          });
          this.$router.push({ name: "home" });
        } else {
          this.$message({
            showClose: true,
            message: msg,
            type: "error"
          });
        }
      });
    }

使用await的方式
簡記在函數的前面使用async
在請求時,使用await。然后用一個變量進行接收哈。

     async handleLogin() {
      const res = await this.$http.post("login", this.formLabelAlign);
      const {
        data,
        meta: { msg, status }
      } = res.data;
      if (status === 200) {
        this.$message({
          showClose: true,
          message: msg,
          type: "success"
        });
        this.$router.push({ name: "home" });
      } else {
        this.$message({
          showClose: true,
          message: msg,
          type: "error"
        });
      }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM