簡體   English   中英

vue:語法錯誤:await 是保留字

[英]vue:Syntax Error: await is a reserved word

async checkDriver(mobile) {
    this.$axios({
      url: "xxx",
      method: "POST",
      data: {
        mobile: mobile
      }
    }).then(res => {
      console.log("========="+res.status);
      return res.data.status;
    }).catch(error => {
      this.$message.error(error.toString());
      return -1;
    });
  },
  addValidate() {
    this.$refs['form'].validate((valid) => {
      if (valid) {
        let status = await this.checkDriver(this.form.mobile);
        console.log(status);

      } else {
        return false;
      }
    });

未解析的變量或類型 await.Highlights 可能旨在異步但缺少 async 修飾符的函數。如何在 => 中使用 await?給我一些幫助。謝謝

async關鍵字必須用於在其主體中使用 await 的函數。

因此,將asynccheckDriver移動到addValidate

checkDriver(mobile) {
    // ...
}
async addValidate() {
    // ...
    let status = await this.checkDriver(this.form.mobile);
}

此外, checkDriver方法應該返回一個承諾。 因此將checkDriver的內容checkDriver為:

checkDriver(mobile) {
    return this.$axios({
        url: "xxx",
        method: "POST",
        data: {
            mobile: mobile
        }
    })
}

並且從 Promise(在本例中為 axios)返回的數據將分配給addValidate status

然后,如果您想處理錯誤,您應該將await調用包裝在 try/catch 塊中。

 addValidate() { this.$refs['form'].validate( async (valid) => { if (valid) { let status = await this.checkDriver(this.form.mobile); console.log(status); } else { return false; } });

您可以在參數旁邊添加缺少的async關鍵字

您必須使addValidate()異步,例如async addValidate() { /* ... */ } 您只能在標記為async的函數中使用await

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM