循环对象值angular2本地存储

时间:2017-11-30 18:45:35

标签: angular typescript

我使用Angular2和角度本地存储。 我从LS中放入一些键并通过更改的对象重写它 我的代码:

set() {
    this.subscribtions.push(this.selectedUser);
    this.currentUser["subscribtions"] = this.subscribtions;
   /* this.localStorageService.set('currentUser', this.currentUser as User);*/

  }

ngOnInit() {
this.subscribtions = this.localStorageService.get(`user${this.currentUser["first_name_field"]}`)["subscribtions"];}

(currentUser和selectedUser属于User类)

class User {
 constructor(public first_name_field: string, public second_name_field: string, public  email_field: string, public password_field: string, public dayBirth: number, public monthBirth: number, public  yearBirth: number, public gender: string, public aboutMe: string, public subscribtions: any, public subscribers: any) {
  }
}

但是我有一个错误:循环对象值

谁可以提供帮助?

1 个答案:

答案 0 :(得分:0)

我的第一个建议是使用spread运算符而不是对数组进行推送

set() {
    this.subscribtions = [...this.subscribtions, this.selectedUser];
    this.currentUser["subscribtions"] = this.subscribtions;
   /* this.localStorageService.set('currentUser', this.currentUser as User);*/

  }

改变状态是一种不好的做法,这可能是你问题的根源。