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