简体   繁体   中英

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;
      }
    });

Unresolved variable or type await.Highlights functions that were possibly intended to be async but are missing the async modifier.How to use await in =>?Give me some help.Thanks

The async keyword must be used for the function that USES await in its body.

So, move async from checkDriver to addValidate :

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

Also, the checkDriver method should return a promise. So change the contents of checkDriver to:

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

and the data returned from the Promise (axios in this case) will be assigned to status in addValidate

Then if you want to handle errors, you should wrap the await call in a try/catch block.

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

You can add the missing async keyword beside the parameter

You must make addValidate() async, like async addValidate() { /* ... */ } . You can only use await in a function that's marked async .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM