Access @HostListener properties from inside constructor? Typescript

huangapple go评论64阅读模式
英文:

Access @HostListener properties from inside constructor? Typescript

问题

我试图记录并存储鼠标在页面上的最后移动时间。这有些困难。

我已经阅读了文档https://angular.io/api/core/HostListener,但我不明白为什么@HostListener属性不可用于Typescript文档的其余部分。

```typescript
import { Component, HostListener, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-nav-menu',
  templateUrl: './nav-menu.component.html',
  styleUrls: ['./nav-menu.component.css']
})
export class NavMenuComponent {
  isExpanded = false;
  active: boolean = false;
  counter: any = 10;
  document: any = 0;
  dateTimeNow: Date;

  @HostListener('document:mousemove', ['$event'])
  onMouseMove(e: any) {
    console.log(e, this.dateTimeNow = new Date());
  }

  constructor(private router: Router) {
    let intervalId = setInterval(() => {
      this.counter = this.counter - 1;
      console.log(this.counter);
      var a = this.onMouseMove;
      console.log(a);

      if (this.counter === 3) {
        this.counter = 10;
      }

      if (this.counter === 0) {
        clearInterval(intervalId);
        this.router.navigateByUrl('/');
      } 
    }, 1000)
  }
}
英文:

I'm trying to log and store the last time a mouse move occurred on the page. Proving Difficult.

I've read the docs https://angular.io/api/core/HostListener but I'm cannot understand why the @HostListener properties aren't available to the rest of the Typescript document.

import { Component, HostListener, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-nav-menu',
  templateUrl: './nav-menu.component.html',
  styleUrls: ['./nav-menu.component.css']
})
export class NavMenuComponent {
  isExpanded = false;
  active: boolean = false;
  counter: any = 10;
  document: any = 0;
  dateTimeNow: Date;

  @HostListener('document:mousemove', ['$event'])
  onMouseMove(e: any) {
    console.log(e, this.dateTimeNow = new Date());

  }

  constructor(private router: Router) {

    //this.dateTimeNow = new Date();
    //rather than setting a new date I want the date from inside of the @HostListener
    //then store this in another variable.   

    let intervalId = setInterval(() => {

      this.counter = this.counter - 1;

      console.log(this.counter);

      var a = this.onMouseMove;
      console.log(a);


      if (this.counter === 3) {
        this.counter = 10;

      }

      if (this.counter === 0) {
        clearInterval(intervalId);

        this.router.navigateByUrl('/');

      } 

    }, 1000)

  }

答案1

得分: 1

使用AfterViewInit()钩子,然后您可以确保DOM可用。在变更检测期间,Angular会自动更新数据的值。

英文:

Use the AfterViewInit() hook, then you can be sure the dom is available.
During change detection, Angular automatically updates the data with the value.

huangapple
  • 本文由 发表于 2023年2月8日 20:53:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定