Issue
I got error core.js:6241 ERROR TypeError: this.test is not a function at klass.<anonymous> when try to call function in canvas
HTML
<canvas #canvas id="canvas" width="900" height="400"></canvas>
ts
ngOnInit() {
this.cvs.on("stagemouse:down", function(){
console.log('Event mouse:down Triggered');
this.test();
})
}
test(){
console.log("test");
}
Solution
When defining a function with function(), it is not "bound", meaning the value of this might not always be the same depending on when and where the function is called. To declare a "bound" function, use the arrow syntax:
this.cvs.on("stagemouse:down", () => {
console.log('Event mouse:down Triggered');
this.test();
});
This syntax makes this in the function always refer to the this that is currently there when it is created.
Answered By - CoderCharmander
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.