我在文档中找到了订阅鼠标移动的代码,如何才能只订阅组件呢?
ngOnInit() {
this.subscription =
fromEvent(document, 'mousemove')
.subscribe(e => {
console.log(e);
});
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
最佳答案
使用元素引用
ElementRef grants direct access to the host DOM element through its nativeElement property.
constructor(private ele: ElementRef ){
const host = fromEvent(this.ele.nativeElement,'click').subscribe(c=>console.log(c));
}
关于angular - 订阅组件鼠标移动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57370907/