One issue I'm facing is with a function called doSomething() within my Vue component. It contains an event handler named onmessage() that seems to be unable to access any data or methods within the component itself. This means I am unable to reach the component's variables or functions from within onmessage(). How can I solve this problem and gain access to the component's data from within that specific function?
export default {
name: "component",
data() {
return {
name: "Jeb",
connection: null
}
},
methods: {
doSomething: function() {
this.connection = new WebSocket("ws://localhost:2000");
this.connection.onmessage = function(event) {
console.log(this.name); //UNDEFINED
this.doAnotherThing(); //ERROR
};
...
...
},
doAnotherThing: function() {
...
}
}
};
I regret any inconvenience caused by repeating this question, as I have tried searching for a solution extensively but couldn't find a relevant post addressing the same issue.