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?
It depends on how you call your
postQuestionsContent
method (if you call it asynchronously, you might need tobind
thethis
context).In most cases, you should be able to access it using
this.$data.YOURPROPNAME
, in your casethis.$data.sendButtonDisable
:Try this instead
Registering your methods in the above manner should resolve the issue.
Inside methods if you don't have another scope defined inside, you can access your data like that:
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:
complete example: