【发布时间】:2018-09-17 08:32:58
【问题描述】:
我被这个错误困扰了几天,我不知道如何解决它。
我的应用中有 2 个模型:User 和 Team。
user.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);
}
team.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
是否可以保留此逻辑并避免循环依赖警告?
非常感谢!
【问题讨论】:
-
您的问题的答案是“不,您不应该保持这种状态”。循环依赖意味着你有错误的设计,你需要修复它。从您提供的信息中我可以肯定地说。
标签: angular model warnings circular-dependency