我的意思是,我想跟踪用户何时离开应用程序,关闭浏览器或标签.
组件和指令有一个名为ngOnDestroy的生命周期钩子,它在组件被销毁时被调用,但是当用户离开应用程序时它无法捕获
import { Component, OnInit } from '@angular/core';
@Component({
  moduleId: module.id,
  selector: 'app',
  templateUrl: 'app.component.html'
})
export class AppComponent implements OnDestroy {
  constructor() { }
  ngOnDestroy() {
      alert(`I'm leaving the app!`);
  }
}
如果用户关闭浏览器,则不会执行警报.
Gün*_*uer 42
你可以听unload或beforeunload类似这样的事件:
export class AppComponent {
  @HostListener('window:unload', [ '$event' ])
  unloadHandler(event) {
    // ...
  }
  @HostListener('window:beforeunload', [ '$event' ])
  beforeUnloadHander(event) {
    // ...
  }
}
也可以看看
| 归档时间: | 
 | 
| 查看次数: | 29249 次 | 
| 最近记录: |