Async/await axios calls with Vue.js

后端 未结 2 1128
天涯浪人
天涯浪人 2021-02-07 21:16

I\'m having a little trouble setting one of my this. values within my Vue.js application. I believe I\'m either not understanding async axios calls correctly, or how async works

2条回答
  •  感情败类
    2021-02-07 21:44

    Why use promises pattern if you are using async await. This removes the use of callbacks and this binding being lost

    You can do it like this

    handleAvailabilityRequest: async function (event) {
      event.preventDefault();
        ...
    
      try{
       let response =  await axios.post('{{ request_absolute_uri }}', formData, config)
          this.availabilityMessage = response.data.message;
      }catch(error) {
          this.availabilityMessage = false;
          console.log(error);
        };
      }
      return this.availabilityMessage;
    }
    

    You can use try/catch block for handling errors when using async/await

提交回复
热议问题