Angular4x : ngx-cookie-service with expire parameter

后端 未结 3 1366
忘掉有多难
忘掉有多难 2021-01-18 10:20

I am using ngx-cookie-service component but as soon as i close the browser the cookies disappears, maybe i have to set the expire parameter but i can\'t get it , below what

相关标签:
3条回答
  • 2021-01-18 10:35

    I've solved it like that

    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'));   
    

    Remember to inject the cookieService

      import { CookieService } from 'ngx-cookie-service';
      constructor(private cookieService: CookieService) {}
    

    Install

    npm install angular2-cookie --save
    
    0 讨论(0)
  • 2021-01-18 10:42

    Which browser are you using? Did you check your browser cookie settings? It might be set to delete all cookies when closing the browser window.

    0 讨论(0)
  • 2021-01-18 10:54

    Use number, which is the 3rd parameter, where the number represents the number of days until it expires.

    For example, if you want the cookie to expire in 365 days, use:

    this.cookieService.set('cookieName', cookieValue, 365);
    

    Hope that helps!

    0 讨论(0)
提交回复
热议问题