Accessing VUE JS's data from Axios

前端 未结 2 1949
再見小時候
再見小時候 2020-12-08 17:57

I have a Vue JS (Vuetify) App that makes an ajax request that I would like to populate a div\'s content with the response, however I am having difficulties accessing the ins

2条回答
  •  情深已故
    2020-12-08 18:24

    I can think of these solutions for your problem.

    1) You can create a reference to this and use it.

    send: function() {
      let self = this
      axios.post(this.api + "orders", this.order).then(function(response) {
        self.message = "Your payment was successful"
      }
    }
    

    2) An arrow function will enable you to use this which will point to your Vue instance.

    send: function() {
      axios.post(this.api + "orders", this.order).then(response => {
        this.message = "Your payment was successful"
      }
    }
    

    3) Use bind to assign an object to this which will be the current Vue instance in your case.

    send: function() {
      axios.post(this.api + "orders", this.order).then(function(response) {
        this.message = "Your payment was successful"
      }.bind(this))
    }
    

提交回复
热议问题