如何保持cookie值保存在angular 2中

时间:2019-01-18 23:32:03

标签: javascript node.js angular cookies session-cookies

我将此Cookie保存在应用程序组件中,如下所示:

export class AppComponent {
  item=new Array();

  constructor(private Cookies:CookieService){
  this.Cookies.set('dislikedShops',${this.item});

  //I am trying to use localStorage but the same as the cookie service
  // localStorage.setItem('dislikedShops',${['Shop1']});
}

ngOnInit() {
}

onShopLiked(shopTitle: String) {
  // this.item = [${localStorage.getItem('dislikedShops')}];
  this.item =new Array(${this.cookieService.get('dislikedShops')});

  //Append New Shop Item to localStorage list
  this.item.push(shopTitle);
  this.cookieService.set('dislikedShops',${this.item});

  //Get shop Details and add it with preferred shops
  console.log(this.cookieService.get('dislikedShops'));
}

当我重新加载页面时,所有cookie值都丢失了,有什么方法可以将这些值保留在浏览器中并按时间对其进行控制,而此cookie在应用程序组件中仅用于初始化,之后,我有了一个主页上有一张卡片列表,其中包含两个按钮,如“喜欢/不喜欢”,当用户按下“不喜欢”按钮时,它将在此cookie中追加卡片的新标题值,所以问题是当我重新加载页面时,cookie丢失了所有值?

0 个答案:

没有答案