【发布时间】:2020-06-30 07:17:58
【问题描述】:
我有一个问题,在我(成功)在 ngAfterViewInit() 中使用变量后,它们都重置了。
我有几个 @Viewchild 和普通变量,它们正在 ngAfterViewInit 中使用或设置,但是当我在 init 之后添加的几个事件被调用时,这些非常相同的变量仅显示为 undefined
代码(略微简化):
// Variable declaration
@ViewChild('imagearea') imageArea: ElementRef;
dragBottomRight: HTMLElement;
ngAfterViewInit() {
this.dragBottomRight = this.imageArea.nativeElement.querySelector('.br');
this.imageArea.nativeElement.addEventListener("mousedown", this.dragStart);
// This correctly logs the elements
console.log(this.imageArea, this.dragBottomRight);
}
dragStart(e: MouseEvent) {
// This logs: undefined, undefined
console.log(this.imageArea, this.dragBottomRight);
}
我尝试将变量设置为静态,我的同事似乎可以使用几乎相同的代码结构。
非常感谢任何帮助
【问题讨论】:
-
如何附加
dragStart事件监听器?
标签: javascript angular typescript