根据要求,当他关闭浏览器上的最后一个选项卡时,我必须注销用户。
ngOnInit() {
let counter: any = this.cookieService.get('screenCounterCookie');
counter ? ++counter : (counter = '1');
this.cookieService.set('screenCounterCookie', counter);
}
@HostListener('window:beforeunload', ['$event'])
ngOnDestroy() {
let counter: any = this.cookieService.get('screenCounterCookie');
if (counter > 1) {
--counter;
this.cookieService.set('screenCounterCookie', counter);
} else {
this.cookieService.delete('screenCounterCookie');
window.open(environment.cognitoLogoutURL);
}
}
行为是不稳定的。有时它会减少计数器,有时不会。此外,我必须在这里处理刷新、多个选项卡关闭和浏览器关闭逻辑。我该如何实现?
最佳答案
保持标签总数:https://jsbin.com/mipanuro/1/edit?html,js,output (引用这个)
方法 1(将进行 POST
调用):
使用 BeaconAPI ( navigator.sendBeacon()
)。这将在后台发生。
方法 2(也适用于 GET
调用):
使用 fetch
和 keep-alive
方法三(Websocket):
每隔几分钟左右 Ping 一下您的后端。如果没有 ping,后端可能会使您的 session 无效。
关于angular - 关闭最后一个选项卡后重定向到注销链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66634196/