首页 > 解决方案 > 如何在 Angular 6+ 中检测浏览器关闭事件或标签关闭事件?

问题描述

我已经@HostListener在我的 AppComponent 类中编写来检测浏览器关闭或标签关闭。

我已经编写了这段代码,但它不起作用。请帮助我理解为什么我的代码不起作用

下面是我的代码。

export class AppComponent {

  @HostListener('window:unload', [ '$event' ])
  unloadHandler(event) {
    alert('call unload');
  }

  @HostListener('window:beforeunload', [ '$event' ])
  beforeUnloadHander(event) {
    alert('call beforeunload');
  }
}

标签: javascriptangulartypescriptangular6angular5

解决方案


推荐阅读