如何在 Angular 6+ 中检测浏览器关闭事件或选项卡关闭事件?

问题描述 投票:0回答:1

我在 AppComponent 类中编写了

@HostListener
来检测浏览器关闭或选项卡关闭。

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

下面是我的代码。

export class AppComponent {

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

  @HostListener('window:beforeunload', [ '$event' ])
  beforeUnloadHander(event) {
    alert('call beforeunload');
  }
}
javascript angular typescript angular6 angular5
1个回答
0
投票

使用Angular守卫,Angular中有5种类型的守卫,即CanActivate、CanActivateChild、CanDeactivate、Resolve和CanLoad

© www.soinside.com 2019 - 2024. All rights reserved.