What is Vue way to access to data from methods?

Javascriptvue.jsMethods

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

Inside methods if you don't have another scope defined inside, you can access your data like that:

this.sendButtonDisable = true; 

but if you have a scope inside the function then in vue is a common usage of a variable called vm (stands for view model) at the beginning of the function, and then just use it everywhere like:

vm.sendButtonDisable = false;

An example of vm can be seen in the Vue official documentation as well.

complete example:

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

methods: { 
  postQuestionsContent : function() {
    // This works here.
    this.sendButtonDisable = true;

    // The view model.
    var vm = this;

    setTimeout(function() {
      // This does not work, you need the outside context view model.
      //this.sendButtonDisable = true;
      
      // This works, since wm refers to your view model.
      vm.sendButtonDisable = true;
    }, 1000); 
  }
}

Solution 2 - Javascript

It depends on how you call your postQuestionsContent method (if you call it asynchronously, you might need to bind the this context).

In most cases, you should be able to access it using this.$data.YOURPROPNAME, in your case this.$data.sendButtonDisable:

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

  },

  methods: 
  { 
     postQuestionsContent : function()
     {
        this.$data.sendButtonDisable = true;
     }
  }

Solution 3 - Javascript

Try this instead

...
methods: 
{ 
   postQuestionsContent ()
   {
      this.sendButtonDisable = true;
   }
}

Registering your methods in the above manner should resolve the issue.

Solution 4 - Javascript

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{
						//...
					}
				}
		});
	}
},

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDmitry BubnenkovView Question on Stackoverflow
Solution 1 - JavascriptV. SamborView Answer on Stackoverflow
Solution 2 - JavascriptnilsView Answer on Stackoverflow
Solution 3 - JavascriptThe OracleView Answer on Stackoverflow
Solution 4 - Javascriptajax333221View Answer on Stackoverflow