gpt4 book ai didi

Angular 5 : Circular dependency detected between models

转载 作者:行者123 更新时间:2023-12-03 22:31:54 30 4
gpt4 key购买 nike

我被这个错误困了几天,我不知道如何解决。

我的应用中有 2 个模型:UserTeam .

用户.ts:

import { Team } from './team';

export class User {

id: string = null;
name: string = null;
email: string = null;
settings: any = {};

team: Team = null;

constructor(json?: Object){
var defaultSettings = {};

if(json){
this.id = json['id'] || null;
this.name = json['name'] || null;
this.email = json['email'] || null;
this.settings = json['settings'] || {};

this.team = new Team(json['team']) || null;
}
}

getSettings(){
return Object.assign(this.team.settings, this.settings);
}

团队.ts
import { User } from './user';

export class Team {

id: string = null;
name: string = null;
settings: any = {};

users: User[] = [];

constructor(json?: any){
if(json){
this.id = json['id'] || null;
this.name = json['name'] || null;
this.settings = json['settings'] || {};

if(json['users']) json['users'].forEach(user => this.users.push(new User(user)));
}
}

}

当用户登录时,我得到了他与团队的信息。像这样,我可以做 user.getSettings()直接从用户获取这些设置和团队的合并数组。

另一方面,当我展示一个团队时,它可以有一些用户。

但是有了这个,我得到了一个警告:
WARNING in Circular dependency detected:
src/app/_models/user.ts -> src/app/_models/team.ts -> src/app/_models/user.ts

是否可以保持这种逻辑并避免循环依赖警告?

非常感谢 !

最佳答案

几天后,我终于创建了第三个模型 “登录用户” ,它扩展了我的 “用户”模型,带有 “团队:团队”属性(property) :

用户.ts:

export class User {

id: string = null;
name: string = null;
email: string = null;
settings: any = {};

constructor(json?: Object){
var defaultSettings = {};

if(json){
this.id = json['id'] || null;
this.name = json['name'] || null;
this.email = json['email'] || null;
this.settings = json['settings'] || {};
}
}
}

团队.ts:
import { User } from './user';

export class Team {

id: string = null;
name: string = null;
settings: any = {};

users: User[] = [];

constructor(json?: any){
if(json){
this.id = json['id'] || null;
this.name = json['name'] || null;
this.settings = json['settings'] || {};

if(json['users']) json['users'].forEach(user => this.users.push(new User(user)));
}
}
}

登录用户.ts :
import { User } from './user';
import { Team } from './team';

export class LoggedUser extends User {

team: Team = null;

constructor(json?: Object) {
super(json);

this.team = new Team(json['team']) || null;
}

getSettings(){
return Object.assign(this.team.settings, this.settings);
}
}

关于 Angular 5 : Circular dependency detected between models,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49072091/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com