如何在 Angular 中关闭浏览器选项卡时清除会话存储?



当我使用浏览器历史记录时,它会登录。当我在新选项卡中输入 URL时,它会被重定向到登录页面。我已经尝试过浏览器选项卡关闭事件来清除会话。

// 1
@HostListener('window:unload', ['$event'])
// 2
@HostListener('window:beforeunload')

但它的工作选项卡关闭并刷新页面。我只需要关闭浏览器选项卡。如何实现?

你必须像这样实现 OnDestroy 接口

@Component({selector: 'your-cool-comp', template: `...`})
class MyComponent implements OnDestroy {
ngOnDestroy() {
// your logic to clean up session
}
}

您在单击事件或销毁事件上编写此代码

if(windows.sessionStorage){ sessionStorage.clear(); }

最新更新