为什么关闭浏览器后cookies没有保存?

Mic*_*ael 1 cookies angularjs

我在我的项目中使用 angularjs1.5.5。

我在我的项目中使用 $cookies 服务。我在 cookie 中创建并存储一些凭据:

this.setCookie = function (inspectorName) {
    $cookies.put('inspectorName', inspectorName, {
        expires: new Date(new Date() + 1, new Date().getMonth(), new Date().getDate())
    });
}
Run Code Online (Sandbox Code Playgroud)

在这里我如何访问 cookie:

var credentials = $cookies.get('inspectorName');
Run Code Online (Sandbox Code Playgroud)

但是当我关闭并打开页面并尝试按上面的行获取cookie时,我得到“未定义”。

知道为什么关闭浏览器后 cookie 没有保存吗?

Nik*_*nan 5

new Date(new Date() + 1, new Date().getMonth(), new Date().getDate())
Run Code Online (Sandbox Code Playgroud)

expires 值变为 null。所以它把到期时间作为浏览器会话。意味着 cookie 的生命将在浏览器关闭时到期。添加一个有效的未来日期,我相信它会起作用。