无法使用 ngx-cookie-service 设置 cookie

Couldn't set cookie using ngx-cookie-service

我正在使用 angular 9,我需要设置一个 cookie。我正在使用 ngx-cookie-service 3.0.4 并尝试按以下方式进行操作:

this.cookieService.set("cookieName", user.tokenId, date, "/", "localhost:4200");

当我尝试取出这个时,我在响应中得到 null 并且在浏览器中我没有看到这个 cookie。我正在尝试得出如下结果:

console.log("COOKIE: ", this.cookieService.get("cookieName));

我做错了什么?

为localhost设置cookies时,不能使用'localhost:4200'作为域。这是设计使然,您可以在此处阅读更多相关信息:Cookies on localhost with explicit domain

在这种情况下,当应用程序在本地主机上 运行 时,您可以传入 null 或完全省略域。

const hostName = isLocal ? null : 'HOST_NAME';
this.cookieService.set("cookieName", user.tokenId, date, "/", hostName);