角度5:在模型之间检测到循环依赖

编程入门 行业动态 更新时间:2024-10-28 14:25:33
本文介绍了角度5:在模型之间检测到循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这个错误让我呆了几天,我也不知道如何解决.

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:在模型之间检测到循环依赖

本文发布于:2023-10-30 21:55:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544115.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:检测到   模型   角度

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!