所以我需要在Angular 2或4中管理我的应用程序的浏览器选项卡是否关注.有没有办法使用window.onfocus和window.onblur?
非常感谢
解决方法@H_403_6@
您可以将组件与@HostListener一起使用.
就像是:
@Component({})
export class WindowComponent {
constructor(){}
@HostListener('window:focus',['$event'])
onFocus(event: any): void {
// Do something
}
@HostListener('window:blur',['$event'])
onBlur(event: any): void {
// Do something
}
}
只需检查您是否同时运行多个WindowComponent,因为您将遇到意外行为,因为每个实例都会对这些事件做出反应.
就像是:
@Component({}) export class WindowComponent { constructor(){} @HostListener('window:focus',['$event']) onFocus(event: any): void { // Do something } @HostListener('window:blur',['$event']) onBlur(event: any): void { // Do something } }
只需检查您是否同时运行多个WindowComponent,因为您将遇到意外行为,因为每个实例都会对这些事件做出反应.