【问题标题】:Cookies not deleting all the time angular 7Cookies不会一直删除角度7
【发布时间】:2019-05-28 10:34:23
【问题描述】:

我使用 ngx-cookie-service 来存储我的令牌,但是当我单击断开连接时,不会每次都删除 cookie。有时它可以工作,但有时却不行。

有时我只需要重新加载页面以确保删除 cookie,有时它可以正常工作,但它不会将我重定向到登录页面。 我在 localhost 和构建相同的东西中对其进行了测试。 对于我使用 chrome 的浏览器

要设置我的令牌,我使用这个:

 setAuth(value, expireTime): void {
this.cookieService.set('id_token', value, expireTime, '../');

}

我正在使用以下代码删除 cookie:

clearCookies(){this.cookieService.deleteAll('../');}

这是我的注销功能:

  logOut() {
let path = location.pathname;
if (path.indexOf('/panier') > -1 || path.indexOf('/store') > -1) {
  this.setLogout({ value: true });
} else {
  this.disconnect().subscribe(res => {
    if (res.status == 'success') {
      this.setLogout({ value: false })
      this.clearCookies();
      this.router.navigate(['/login'])
    }
  })
}

}

【问题讨论】:

    标签: cookies angular7


    【解决方案1】:

    我在 Angular8 中遇到了同样的问题(之前也在 Angular 6 和 7 中)。我在文档中找到了答案:https://www.npmjs.com/package/ngx-cookie-service

    显然 cookieservice 通过路径保存 cookie。因此,如文档中所述,如果您不确定应该删除哪个路径:添加'/'

    我使用了以下方法

    logout(): void {
            this.cookieService.delete('TESTCOOKIE', '/');
        }
    

    这对我有用。

    【讨论】:

      【解决方案2】:

      你可以试试关注

       logout() {
                  this.cookieService.deleteAll('/', 'xyz.net');       
              }
      

      这里 '/' 是 Path 和 xyz.net 域名,这是您在设置 Cookie 时指定的 这段代码对我有用。我猜它会删除所有具有相同域的cookie

      【讨论】:

        【解决方案3】:

        使用

        this.cookieService.deleteAll('/', domainName); 
        

        而不是

        this.cookieService.deleteAll('../')
        

        this.cookieService.deleteAll()
        

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 1970-01-01
        • 2021-06-17
        • 2015-12-08
        • 1970-01-01
        • 2011-01-01
        • 1970-01-01
        • 2019-07-21
        • 2018-02-12
        相关资源
        最近更新 更多