programing

vue: 구문 오류: wait는 예약된 단어입니다.

minecode 2022. 8. 18. 22:59
반응형

vue: 구문 오류: wait는 예약된 단어입니다.

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

확인되지 않은 변수 또는 유형이 대기 중입니다.비동기화할 가능성이 있지만 비동기 수식자가 없는 함수를 강조 표시합니다.=>에서 wait를 사용하는 방법나 좀 도와줘.고마워요.

asyncUSE가 본문에서 대기하는 함수에는 키워드를 사용해야 합니다.

자, 움직이세요async부터checkDriver로.addValidate:

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

또,checkDriver메서드는 약속을 반환해야 합니다.따라서 의 내용을 변경합니다.checkDriver대상:

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

Promise(이 경우 축)에서 반환된 데이터는 다음 주소로 할당됩니다.statusaddValidate

에러를 처리하려면 , 를 랩 합니다.await시도/시도 블록을 호출합니다.

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() { /* ... */ }. 마크가 붙어 있는 기능에서만 사용할 수 있습니다.

언급URL : https://stackoverflow.com/questions/50925113/vuesyntax-error-await-is-a-reserved-word

반응형