【问题标题】:How to Find the Browser Tab is Focused or Not using HostListener in Angular 2+如何在 Angular 2+ 中使用 HostListener 查找浏览器选项卡是否集中
【发布时间】:2019-01-26 06:03:27
【问题描述】:

我想要使用 HostListener 的浏览器选项卡焦点和模糊事件。

【问题讨论】:

    标签: angular focus blur


    【解决方案1】:

    首先在您的组件中导入HostListener

    import { HostListener } from '@angular/core';
    

    然后把这段代码拿到 Tab Focus and Blur Event

    export class AppComponent { 
    
       @HostListener('window:focus', ['$event'])
       onFocus(event: FocusEvent): void {
    
           // Do something      
    
       }
    
       @HostListener('window:blur', ['$event'])
       onBlur(event: FocusEvent): void {
    
          // Do something
    
       }
    
      ........
      .....
    }
    

    【讨论】:

    • 既然知道事件的类型,为什么还要用any 键入事件?
    猜你喜欢
    • 2019-11-03
    • 1970-01-01
    • 2017-12-17
    • 2018-10-12
    • 2021-03-01
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    相关资源
    最近更新 更多