What is Vue way to access to data from methods?

Viewed 123079

I have the following code:

{
  data: function ()  {
    return {
      questions: [],
      sendButtonDisable: false,
    }
  },

  methods: { 
    postQuestionsContent: function () {
      var that = this;
      that.sendButtonDisable = true;
    },
  },
},

I need to change sendButtonDisable to true when postQuestionsContent() is called. I found only one way to do this; with var that = this;.

Is there a better solution?

4 Answers

I tried both this.$data.sendButtonDisable and vm.sendButtonDisable and did not work for me.

But I got it working with outer_this = this, something like:

methods: {
    sendForm(){
        var outer_this;
        
        outer_this = this;
        
        $.ajax({
                url: "email.php",
                type: "POST",
                dataType: "text",
                data: {
                    abc: "..."
                },
                success: function(res){
                    if(res){
                        //...
                        
                        outer_this.sendButtonDisable = false;
                    }else{
                        //...
                    }
                }
        });
    }
},
Related