It is possible to create tree / recursive objects in separate files and avoid Circular dependency detected warning using Angular CLI if I call methods from classes recursive?
(Tested in angular 7) I understand that this is a problem in dependency injection (article), but what about the objects using as constructor for data from external api or for internal purposes? And if it is not good idea anyway, how do you solve it? (I suppose the answer for this 'subquestion' is a change of model, then how should I modify my model?) I search for solution but the closest question did not help me apply the same approach to my situation.
I have Emails and Attachments (emails with attachments - and the entire Email with attachments can also be an attachment, Attachment is separated object with attributes and content can be File or Email, but here, for simplicity, suppose the attachment is always one Email or null).
Each class has static method for create instance from JSON object (I get JSON from API, then I will create instance of object representing API values by calling static fromJson method on top object):
file: app/model/email.ts (part of code)
import { Attachment } from './attachment';
export class Email {
// some attributes
public attachment?: Attachment;
public static fromJson(js: JSON): Email {
const e = new Email();
// fill some attributes here
if (js['attachment']) { e.attachment = Attachment.fromJson(js['attachment']); }
return e;
}
}
file: app/model/attachment.ts (part of code)
import { Email } from './email';
export class Attachment {
// some attributes
public content: Email;
public static fromJson(js: JSON): Attachment {
const a = new Attachment();
// fill some attributes here
if (js['email']) { a.content = Email.fromJson(js['email']); }
return a;
}
}
Initial call is: this.email = Email.fromJson(apiResponseForEmail);
This situation result into Circular dependency detected warning after ng build | serve. If this is not a good example for you, you can imagine Recipe class for food which contains Ingredients with attributes such as quantity and temperature where the result of one Recipe is a ingredient for another recipe.
If these classes are in one file, no warning is reported. However, placing everything in one big file is not the best solution to me (if there is any other).
Here is a Stackblitz with Circular dependency demo (but without warnings in console, stackblitz obviously removes warnings, you need to download this code and run from localhost to see warning).
I am interested if it is possible to place such classes (which are called in the code recursive by static method like in the example) in separate files (files named by classes) and avoid Circular dependency detected message?