角度5:在模型之间检测到循环依赖 [英] Angular 5: Circular dependency detected between models
问题描述
这个错误让我呆了几天,我也不知道如何解决.
I'm stuck for few days with this error, and I don't know how to solve it.
我的应用程序中有2个模型:User
和Team
.
I have 2 models in my app : User
and 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()
,并获得这些设置和团队的合并数组.
When the user is logged in, I got his infos with the team. Like that, I can do user.getSettings()
directly from the User, and get a merged array of these settings and the Team.
另一方面,当我向团队展示时,它可以有一些用户.
In the other hand, when I show a Team, it can have some users.
但是,我得到了警告:
WARNING in Circular dependency detected:
src/app/_models/user.ts -> src/app/_models/team.ts -> src/app/_models/user.ts
WARNING in Circular dependency detected:
src/app/_models/user.ts -> src/app/_models/team.ts -> src/app/_models/user.ts
是否可以保持这种逻辑并避免循环依赖警告?
Is it possible to keep this logic and avoid the Circular dependency warning?
非常感谢!
推荐答案
几天后,我终于创建了第三个模型"LoggedUser" ,该模型扩展了我的"User" 模型,并具有团队:团队" 属性:
After few days, I've finally created a third model "LoggedUser", which extends my "User" model, with the "team: Team" property :
user.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'] || {};
}
}
}
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)));
}
}
}
loggedUser.ts:
loggedUser.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);
}
}
这篇关于角度5:在模型之间检测到循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!