我的意思是,我想跟踪用户何时离开应用程序,关闭浏览器或标签.
组件和指令有一个名为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!`);
}
}
Run Code Online (Sandbox Code Playgroud)
如果用户关闭浏览器,则不会执行警报.
angular ×1