自己的toJSON将对象对象放置在本地存储中

时间:2019-05-11 16:46:42

标签: json angular typescript local-storage

我需要我的登录用户,并且即使刷新后也要保持他的身份,我将其值放到了本地存储中。当我使用JSON.stringify()时,它将正确的值放置在本地存储中,但是在键名前面加上_,我不知道为什么(我将自己的私有属性_id命名为egid,但是我写了getters,我认为应该再次正常命名吗?)。因为它不起作用,所以我编写了自己的toJSON方法:

toJSON(): any {
    return {
      id: this._id,
      firstName: this._firstName,
      lastName: this._lastName,
      email: this._email,
      phoneNumber: this._phone,
      country: this._country,
      comments: this._comments.map(c => c.toJSON)
    };

但是当我称它为“对象对象”时,它将[对象对象]放置到本地存储中,我不知道为什么会这么做。

我基本上需要使JSON.stringify将我的属性放置在localStorage中,或者我需要修复toJson。

用户模型:

import { Comment } from '../image/comment.model';

export class User {
  constructor(
    private _id: number,
    private _firstName: string,
    private _lastName: string,
    private _email: string,
    private _phone: string,
    private _country: string,
    private _comments: Comment[]
  ) {}

  get id(): number {
    return this._id;
  }

  get firstName(): string {
    return this._firstName;
  }

  get lastName(): string {
    return this._lastName;
  }

  get email(): string {
    return this._email;
  }

  get phone(): string {
    return this._phone;
  }

  get country(): string {
    return this._country;
  }

  get comments(): Comment[] {
    return this._comments;
  }

  static fromJSON(json: any): User {
    return new User(
      json.id,
      json.firstName,
      json.lastName,
      json.email,
      json.phoneNumber,
      json.country,
      json.comments
    );
  }

  toJSON(): any {
    return {
      id: this._id,
      firstName: this._firstName,
      lastName: this._lastName,
      email: this._email,
      phoneNumber: this._phone,
      country: this._country,
      comments: this._comments.map(c => c.toJSON)
    };
  }
}

身份验证服务

this.getUser(email).subscribe(usr => { //this gets my user via its email from the backend
              localStorage.setItem('visitor', usr.toJSON());
              this._loggedInUser$.next(usr);
            });

this._loggedInUser$ = new BehaviorSubject<User>(
      localStorage.getItem('visitor')
        ? User.fromJSON(localStorage.getItem('visitor'))
        : null
    );

  get loggedInUser$(): BehaviorSubject<User> {
    return this._loggedInUser$;
  }

评论

export class Comment {
  private _id: number;
  constructor(
    private _author: string,
    private _content: string,
    private _date: Date,
    private _imageId: number,
    private _visitorId: number
  ) {}

  get id(): number {
    return this._id;
  }
  set id(value: number) {
    this._id = value;
  }

  get author(): string {
    return this._author;
  }

  get content() {
    return this._content;
  }

  get date(): Date {
    return this._date;
  }

  get imageId(): number {
    return this._imageId;
  }

  get visitorId(): number {
    return this._visitorId;
  }

  static fromJSON(json: any): Comment {
    let comment = new Comment(
      json.author,
      json.content,
      json.date,
      json.myImageId,
      json.visitorId
    );
    comment.id = json.id;
    return comment;
  }

  toJSON(): any {
    return {
      id: this._id,
      author: this._author,
      content: this._content,
      date: this._date,
      myImageId: this._imageId,
      visitorId: this._visitorId
    };
  }
}

如果需要更多代码,请告诉我!

1 个答案:

答案 0 :(得分:0)

这可能是因为您给变量赋予了与JSON对象属性名称相同的名称。如果要删除下划线,请尝试为变量提供一些不同的名称。