首页 文章

如何在会话存储中存储以便在关闭浏览器时删除数据?

提问于
浏览
0

我一直致力于在我的角度应用程序中实现用户身份验证 . 我以一种将令牌存储在本地存储中的方式实现 .

这是我在用户输入用户凭据并按下提交时执行的代码 .

this.http.post('http://localhost:3000/users/login', this.user).subscribe(data => {
  if (data['token']) {
    localStorage.setItem('mean-token', data['token']);
    location.reload();
    this.router.navigate(['/']);
  }
});

我的应用程序将令牌存储在本地存储中,并且在关闭浏览器时不会删除令牌 . 我希望在浏览器关闭时删除令牌 . 我怎样才能做到这一点?


问题更新:我知道我需要将数据存储在会话存储而不是本地存储中,但我无法在Angular 4中找到如何做到这一点 . 我想知道如何使用Angular 4进行开发 .

1 回答

  • 2

    "I want the token to be deleted when the browser is closed."

    localStorage 永远保存会话,但是当浏览器关闭时,SessionStorage关闭会话 .

    https://code.google.com/p/sessionstorage/

相关问题