admin管理员组文章数量:1353179
In Angular 2 application we store the token in local storage, now i want to clear the token on browser closing when user not check the remember me option during login. i did it through unload browser event but problem with that event is not fire when user close the browser from task manager in window.
In Angular 2 application we store the token in local storage, now i want to clear the token on browser closing when user not check the remember me option during login. i did it through unload browser event but problem with that event is not fire when user close the browser from task manager in window.
Share Improve this question edited May 19, 2017 at 13:53 bangash asked May 19, 2017 at 12:45 bangashbangash 4181 gold badge5 silver badges12 bronze badges 1- i already implement 'window:beforeunload' but this event is not fire when we close the browser from task manager , – bangash Commented May 19, 2017 at 13:51
3 Answers
Reset to default 6The following should acplish this...
import { Component, HostListener } from "@angular/core";
@Component({
selector: 'app-root',
templateUrl:"./app/app.ponent.html"
})
export class AppComponent {
@HostListener("window:onbeforeunload",["$event"])
clearLocalStorage(event){
localStorage.clear();
}
}
Note : onBeforeUnload
is executing on browser close event
You can use the window.beforeunload event to clear the local storage.
@HostListener('window:beforeunload', ['$event'])
beforeunloadHandler(event) {
localStorage.removeItem(key);
}
Alternatively you can store your information in session storage which is practically the same as local storage except the data gets cleared when the tab is closed.
The below code only remove token on close and not when the page refreshed
@HostListener('window:unload', ['$event'])
async unloadHandler(event) {
if (event.currentTarget.performance.navigation.type !== PerformanceNavigation.TYPE_RELOAD) {
localStorage.clear();
}
}
本文标签: javascriptclear token when browser is closed in angular 2Stack Overflow
版权声明:本文标题:javascript - clear token when browser is closed in angular 2 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743925850a2562946.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论