stringify无法序列化循环结构

stringify无法序列化循环结构

我总是得到一个错误的标题这样的评论。这是我的类createChildcomponent:

  @Input('father') father: Father = new Father();
  child: Child = new Child();
  submitted = false;
  childList: Array<Child> = [];
  family: Family = new Family();

  constructor(private familyService: FamilyService) {
  }

  ngOnInit() {
  }

  createFamily() {
    this.family.father = new Father();
    this.family.father = this.father;
    this.father.family = this.family;
    this.family.childList = new Array<Child>();
    this.family.childList = this.childList;
    this.familyService.createFamily(cache).subscribe(data => console.log(data), error => console.log(error));
    // this.familyService.createFamily(this.family)
    //   // .subscribe(data => console.log(data), error => console.log(error));
    this.family = new Family();
  }
  addChildToList() {
    this.child.family = this.family;
    this.father.family = this.family;
    this.childList.push(this.child);
  }
}

我也试过解决这个问题,但不知道怎么解决。我想把家庭对象发送到数据库。这是我打字的第一步。谢谢你的帮助。

最佳答案

平的比嵌套的好
我们应该保持我们的状态normalized

const familyMap = {
  1: new Family(),
  2: new Family(),
};

const personsMap = {
  1: new Father(),
  2: new Child(),
};

createFamily() {
  familyMap[1].father = {personId: 1};
  personsMap[1].family = {familyId: 1};
}

关于typescript - Angular6-JSON.stringify无法序列化循环结构,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52220721/

10-12 00:58