[Vuejs]-How to save data in Vue instance

0👍

The problem is that this inside your ajax return function doesn’t refer to the vue instance anymore.

The solution is to save the this keyword into a variable inside the function. Example:

getUserAcc: function ( userID ) {
  var that = this;
  this.user = { _id : userID };
  $.ajax({
    url: "/listuser",
    type: "POST",
    data: this.user,
    success: function(data) {
      that.user = data;
      //Rest of your code
    },
    error: function(err) {
      console.log('error: ',err);
    },
  });
}

Here is more information about the behavior of the keyword this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

Leave a comment