我有一个类似的功能:
mouseClickNav() {
document.getElementById('mouseClickNav').style.width = '319px';
let ctx: CanvasRenderingContext2D = this.canvasTest.nativeElement.getContext('2d');
ctx.canvas.addEventListener('click', (event) => {
var mouseX = event.clientX - ctx.canvas.offsetLeft;
var mouseY = event.clientY - ctx.canvas.offsetTop;
this.mouseClickX = mouseX;
this.mouseClickY = mouseY;
});
可以正常工作,并添加事件侦听器。但是,我似乎无法弄清楚如何在完成使用后将其删除。下一个功能是我要删除监听器的地方:
closeMouseClickNav() {
document.getElementById('mouseClickNav').style.width = '0';
let ctx: CanvasRenderingContext2D = this.canvasTest.nativeElement.getContext('2d');
ctx.canvas.removeEventListener('click', () => { });
}
不确定在这里我在做什么错。我声明了侦听器并添加了它,当我删除它时,它是否必须相同?