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"
        });
      }
    }
posted @ 2020-03-08 20:37  南风晚来晚相识  阅读(1118)  评论(0编辑  收藏  举报