Home > OS >  call function in canvas
call function in canvas

Time:12-27

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");
}

CodePudding user response:

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.

  • Related