我正在使用 sessionStorage 来保存我的用户数据。如果您空闲一段时间(例如:2 分钟)。我需要使 sessionStorage 过期。我怎么过期呢?你能给我一点指导吗?
登录功能
signin() {
this.disableSubmit = true;
return this.loginservice.loginUser(this.model).subscribe(
data => {
if (data) {
this.responseuser = data.response;
;
if (data.response.responseCode === 200) {
window.sessionStorage.setItem('token', JSON.stringify(this.responseuser));
window.sessionStorage.setItem('isLoggedIn', 'true');
}
}
},
error => {
});
}
最佳答案
您可以安装ng2-idle
包并在onTimeout
订阅中实现过期。
这是示例源代码
this.idle.onTimeout.subscribe(() => {
this.idleState = 'Timed out!';
this.timedOut = true;
this.idle.stop();
//prevent init multiple time
this.idle.onTimeout.observers.length = 0;
this.idle.onIdleStart.observers.length = 0;
this.idle.onIdleEnd.observers.length = 0;
// add your code to expire session storage here
});
关于javascript - Angular 6 与 session 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55529297/