Angular 5:在模型之间检测到循环依赖
Angular 5: Circular dependency detected between models
我被这个错误困扰了几天,我不知道如何解决它。
我的应用程序中有 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
是否可以保持这个逻辑并避免循环依赖警告?
非常感谢!
Is it possible to keep this logic and avoid the Circular dependency warning?
通过将此添加到您的 angular-cli。json 您可以摆脱警告。
"defaults": {
"build": {
"showCircularDependencies": false
}
}
几天后,我终于创建了第三个模型 "LoggedUser",它扩展了我的 "User" 模型,与 "team: Team" 属性 :
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 :
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);
}
}
我被这个错误困扰了几天,我不知道如何解决它。
我的应用程序中有 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
是否可以保持这个逻辑并避免循环依赖警告?
非常感谢!
Is it possible to keep this logic and avoid the Circular dependency warning?
通过将此添加到您的 angular-cli。json 您可以摆脱警告。
"defaults": {
"build": {
"showCircularDependencies": false
}
}
几天后,我终于创建了第三个模型 "LoggedUser",它扩展了我的 "User" 模型,与 "team: Team" 属性 :
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 :
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);
}
}