Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WARNING in Circular dependency detected - Angular Cli

https://github.com/angular/angular-cli/pull/6813 Added warnings for circular dependencies, and I know I can turn off all warnings using "showCircularDependencies": false. But I would rather keep the circular dependency warnings on. Is there a pattern that will let me fix the use case below, or is there a way to specifically disable the circular dependency plugin on a particular file?

The simplest scenario is if I have 3 files:

forms.model.ts

import { CustomModel } from './custom.model'; import { CustomForm } from './custom.form';  export class Forms {   items: CustomForm[] = [];   public constructor(models?: CustomModel[]) {     models.forEach(model => this.items.push(new CustomForm(model)));   } } 

custom.model.ts

export class CustomModel {   nestedModels: CustomModel[];     } 

custom.form.ts

import { Forms } from './forms.model'; import { CustomModel } from './custom.model';  export class CustomForm {   nestedForms: Forms;    constructor(model: CustomModel) {     this.nestedForms = new Forms(model.nestedModels);   } } 

This causes the following warnings:

WARNING in Circular dependency detected: src\app\models\custom.form.ts -> src\app\models\forms.model.ts -> src\app\models\custom.form.ts  WARNING in Circular dependency detected: src\app\models\forms.model.ts -> src\app\models\custom.form.ts -> src\app\models\forms.model.ts 

In my actual app there are about 20-30 warnings because of this same pattern. I think the underlying plugin https://github.com/aackerman/circular-dependency-plugin supports exclude patterns, but i'm not sure if theres a way to use this via the angular-cli.

like image 781
Dulitha Ranatunga Avatar asked Nov 24 '17 03:11

Dulitha Ranatunga


People also ask

How do I get rid of circular dependency detection?

To resolve with iterative calculation, see File>Spreadsheet Settings.” Changing Google Sheets' iterative calculation requires you to change your settings. Instead, you can resolve circular dependency by changing the formula in the spreadsheet.

What is circular dependency detected angular?

A cyclic dependency exists when a dependency of a service directly or indirectly depends on the service itself. For example, if UserService depends on EmployeeService , which also depends on UserService . Angular will have to instantiate EmployeeService to create UserService , which depends on UserService , itself.

What causes circular dependency?

A circular dependency occurs when two classes depend on each other. For example, class A needs class B, and class B also needs class A. Circular dependencies can arise in Nest between modules and between providers. While circular dependencies should be avoided where possible, you can't always do so.


Video Answer


1 Answers

The issue is clear :

You are using custom.model.ts into custom.form.ts

and also custom.form.ts into custom.model.ts,

This is called CircularDependencies and that is not good.

Solution :

Just remove import { CustomForm } from './custom.form'; from custom.model.ts

like image 158
Vivek Doshi Avatar answered Sep 19 '22 04:09

Vivek Doshi