0

test2\test2.services.ts

import { test1Component } from '../test1/test1.component';
constructor(public tsoComp: test1Component ) { }
betState

test1.component.ts

import { test2Service } from '../test2/test2.services';
constructor(public test2S: test2Service)
someCode(){
  this.test2S.betState = n;
}

error: WARNING in Circular dependency detected:

src\app\layout\sports\sport\test2\test2.services.ts -> src\app\layout\sports\sport\test1\test1.component.ts -> src\app\layout\sports\sport\test2\test2.services.ts

Why is this wrong??

1
  • 7
    If you use a component inside your service, you are probably doing something not in the right way. Mixing up dependencies and coupling classes. What's the reason you inject the Component? Commented Apr 1, 2019 at 7:23

1 Answer 1

3

When angular will look at your code and try to create Objects for your component and the service, it will do that one by one, lets say it starts with creating the service object first, in the service's constructor, you have mentioned that it need a dependency injection of test1Component component, then it will say okay lets create an Object of test1Component. Now it goes to create an instance of of test1Component but there you say that it needs test2Service so creation of these instances are deadlocked, so the circular dependency.

Usually we don't inject a component in a service, please check your requirements.

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

Comments

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.