简体   繁体   English

使用Angular6我只想在单击浏览器选项卡/浏览器关闭按钮而不是页面刷新时调用Logout()函数

[英]Using Angular6 I want to call Logout() function only when browser tab / browser close button is clicked not page refresh

I have a requirement in my app where i want to call Logout() function only when browser tab / browser close button is clicked using angular6. 我的应用程序中有一个要求,我仅在使用angular6单击浏览器选项卡/浏览器关闭按钮时才想调用Logout()函数。

Not when page navigation, page refresh. 页面导航时不刷新,页面刷新。

I have used onbefeunload and onunload. 我用过onbefeunload和onunload。 but it is not working properly. 但它不能正常工作。

Thanks in Advance. 提前致谢。

What you're looking for is the window:unload event. 您正在寻找的是window:unload事件。

export class AppComponent {
    @HostListener('window:unload', [ '$event' ])
    unloadHandler(event) {
       // Runs whenever the user closes tab or browser
    }

Keep in mind that there's certain restrictions in what kind of code you can run during this event. 请记住,在此事件期间可以运行哪种代码有一定的限制。 One example is an alert box -- this will never be shown when the tab is closed. 一个示例是警报框-关闭选项卡时将永远不会显示该框。

You have to implement window unload method conditionally something like, 您必须有条件地实现窗口卸载方法,例如,

 window.onbeforeunload = function (e) { e = e || window.event; var y = e.pageY || e.clientY; if (y < 0) { console.log("Here you have to call logout."); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM