Access @HostListener properties from inside constructor? Typescript

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

Access @HostListener properties from inside constructor? Typescript

问题

  1. 我试图记录并存储鼠标在页面上的最后移动时间。这有些困难。
  2. 我已经阅读了文档https://angular.io/api/core/HostListener,但我不明白为什么@HostListener属性不可用于Typescript文档的其余部分。
  3. ```typescript
  4. import { Component, HostListener, OnInit } from '@angular/core';
  5. import { Router } from '@angular/router';
  6. @Component({
  7. selector: 'app-nav-menu',
  8. templateUrl: './nav-menu.component.html',
  9. styleUrls: ['./nav-menu.component.css']
  10. })
  11. export class NavMenuComponent {
  12. isExpanded = false;
  13. active: boolean = false;
  14. counter: any = 10;
  15. document: any = 0;
  16. dateTimeNow: Date;
  17. @HostListener('document:mousemove', ['$event'])
  18. onMouseMove(e: any) {
  19. console.log(e, this.dateTimeNow = new Date());
  20. }
  21. constructor(private router: Router) {
  22. let intervalId = setInterval(() => {
  23. this.counter = this.counter - 1;
  24. console.log(this.counter);
  25. var a = this.onMouseMove;
  26. console.log(a);
  27. if (this.counter === 3) {
  28. this.counter = 10;
  29. }
  30. if (this.counter === 0) {
  31. clearInterval(intervalId);
  32. this.router.navigateByUrl('/');
  33. }
  34. }, 1000)
  35. }
  36. }
英文:

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.

  1. import { Component, HostListener, OnInit } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. @Component({
  4. selector: 'app-nav-menu',
  5. templateUrl: './nav-menu.component.html',
  6. styleUrls: ['./nav-menu.component.css']
  7. })
  8. export class NavMenuComponent {
  9. isExpanded = false;
  10. active: boolean = false;
  11. counter: any = 10;
  12. document: any = 0;
  13. dateTimeNow: Date;
  14. @HostListener('document:mousemove', ['$event'])
  15. onMouseMove(e: any) {
  16. console.log(e, this.dateTimeNow = new Date());
  17. }
  18. constructor(private router: Router) {
  19. //this.dateTimeNow = new Date();
  20. //rather than setting a new date I want the date from inside of the @HostListener
  21. //then store this in another variable.
  22. let intervalId = setInterval(() => {
  23. this.counter = this.counter - 1;
  24. console.log(this.counter);
  25. var a = this.onMouseMove;
  26. console.log(a);
  27. if (this.counter === 3) {
  28. this.counter = 10;
  29. }
  30. if (this.counter === 0) {
  31. clearInterval(intervalId);
  32. this.router.navigateByUrl('/');
  33. }
  34. }, 1000)
  35. }

答案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:

确定