2

I'm getting the following warning whenever I run my project:

Circular dependency detected: src/app/Dashboard/dashboard.module.ts -> src/app/Dashboard/finance.service.ts -> src/app/Dashboard/dashboard.module.ts

In my finances.service I have a function that creates an object of type "Statistics" which is a class that I defined on my dashboard.module

dashboard.module

export class Stadistics {
  mod: number;
  min: number;
  max: number;
}

finance.service

 getStatistics(array: Array<number>) {
    const stats = new Statistics;
    stats.max = 0;
    stats.min = 0;
    stats.mod = 0;
}

I also tried using: const stats: Statistics = new Statistics; but I keep getting the same warning. Any idea why this is happening?

1
  • Have you forgotten () after new Statistics? Commented Sep 23, 2017 at 13:30

1 Answer 1

2

Standard approach in such a case is to move declarations causing circular dependencies into separate source file. So, it could look something like this:

statistics.class.ts

export class Statistics {
    mod: number;
    min: number;
    max: number;
}

finance.service.ts

import {Statistics} from './statistics.class';
....
....
....
getStatistics(array: Array<number>) {
    const stats = new Statistics();
    stats.max = 0;
    stats.min = 0;
    stats.mod = 0;
}

dashboard.module.ts

import {Statistics} from './statistics.class';

// use it here where you need it.

Of course, you need to think through very carefully how to split your sources so that it would make sense.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This solved it. Any idea why does this happens? I have a lot of classes in dashboard.module and only Statistics gave this warning.
It doesn't matter how many classes or which classes or other entities are causing it. Circular dependency is on the file level. Maybe this class was picked as first by compiler using some criteria, or maybe this dependency appeared when you first introduced this class... who knows :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.