[Vuejs]-Vue.js – Access data variable from callback function

3πŸ‘

βœ…

Yes you can. You either use arrow functions

DZ.Event.subscribe("track_end", (evt_name) => {
            console.log("event fired 1");
            //trying to access this bool var that is defined in data
            this.isDeezerPlaying = false;
});

or you bind it:

DZ.Event.subscribe("track_end", function (evt_name) {
            console.log("event fired 1");
            //trying to access this bool var that is defined in data
            this.isDeezerPlaying = false;
}.bind(this));
πŸ‘€bill.gates

1πŸ‘

try with below code:

const vm = this;
DZ.Event.subscribe("track_end", function (evt_name) {
   console.log("event fired 1");
   //trying to access this bool var that is defined in data
   vm.isDeezerPlaying = false;
});
πŸ‘€Jinal Somaiya

1πŸ‘

Yes you can by using arrow functions :

DZ.Event.subscribe("track_end", (evt_name) => {
            console.log("event fired 1");
            this.isDeezerPlaying = false;
});

How does the "this" keyword work?

https://stackoverflow.com/a/24900924/5671919

πŸ‘€Pierre Said

Leave a comment