0👍
You have three ways to do this:
On ES5:
-
Add
.bind(this)
at the end of the function, to be able to accessthis
inside the functionchannel.bind('App\\Events\\UserHasNewMessage', function(data) { this.messages.push(data); }.bind(this));
-
Assign
this
to a variable, and use that variable inside the functionvar that = this; channel.bind('App\\Events\\UserHasNewMessage', function(data) { that.messages.push(data); }.bind(this));
On ES6:
-
Use the fat arrow, so you can use
this
inside the functionchannel.bind('App\\Events\\UserHasNewMessage', (data) => { this.messages.push(data); });
Source:stackexchange.com