Error when trying to inject a service into an angular component “EXCEPTION: Can't resolve all parameters for component”, why?

前端 未结 30 1551
无人共我
无人共我 2020-11-22 17:18

I\'ve built a basic app in Angular, but I have encountered a strange issue where I cannot inject a service into one of my components. It injects fine into any of the three o

30条回答
  •  清酒与你
    2020-11-22 17:36

    I also encountered this by injecting service A into service B and vice versa.

    I think it's a good thing that this fails fast as it should probably be avoided anyway. If you want your services to be more modular and re-usable, it's best to avoid circular references as much as possible. This post highlights the pitfalls surrounding that.

    Therefore, I have the following recommendations:

    • If you feel the classes are interacting too often (I'm talking about feature envy), you might want to consider merging the 2 services into 1 class.
    • If the above doesn't work for you, consider using a 3rd service, (an EventService) which both services can inject in order to exchange messages.

提交回复
热议问题