[Vuejs]-Vue.js to display Pusher data Laravel 5.4

0👍

You have three ways to do this:

On ES5:

  1. Add .bind(this) at the end of the function, to be able to access this inside the function

    channel.bind('App\\Events\\UserHasNewMessage', function(data) {
         this.messages.push(data);
    }.bind(this));
    
  2. Assign this to a variable, and use that variable inside the function

    var that = this;
    channel.bind('App\\Events\\UserHasNewMessage', function(data) {
         that.messages.push(data);
    }.bind(this));
    

On ES6:

  1. Use the fat arrow, so you can use this inside the function

    channel.bind('App\\Events\\UserHasNewMessage', (data) => {
         this.messages.push(data);
    });
    

Leave a comment