我正在使用ngx-cookie-service组件,但一旦我关闭浏览器,cookie就会消失,也许我必须设置expire参数,但我无法获取它,如下文档所述:
set( name: string, value: string, expires?: number | Date, path?: string, domain?: string, secure?: boolean ): void;(我尝试使用数字,但似乎不起作用)
提前谢谢。
发布于 2018-04-25 19:29:56
我就是这样解决的
this.expiredDate = new Date();
this.expiredDate.setDate( this.expiredDate.getDate() + 7 );
//write
this.cookieService.set( 'key',JSON.stringify('value'), this.expiredDate);
//read
console.log(JSON.parse(this.cookieService.get( 'key')); 记得注入cookieService
import { CookieService } from 'ngx-cookie-service';
constructor(private cookieService: CookieService) {}安装
npm install angular2-cookie --save发布于 2019-01-22 06:14:43
使用number,这是第三个参数,其中数字表示过期前的天数。
例如,如果您希望cookie在365天后过期,请使用:
this.cookieService.set('cookieName', cookieValue, 365);希望这能有所帮助!
发布于 2018-04-14 05:44:41
您使用的是哪种浏览器?你检查你的浏览器cookie设置了吗?它可能被设置为在关闭浏览器窗口时删除所有cookies。
https://stackoverflow.com/questions/49817792
复制相似问题