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

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


Import it from the file where it is declared directly instead of the barrel.

I don't know what exactly causes the issue but I saw it mentioned several times (probably some kind of circular dependency).

It should also be fixable by changing the order of the exports in the barrel (don't know details, but was mentioned as well)


In addition to the previous answers given, it seems this error is thrown as well when your injectable service is missing the actual @Injectable() decorator. So before you debug the cyclic dependency thing and the order of your imports/exports, do a simple check whether your service actually has @Injectable() defined.

This applies to the current Angular latest, Angular 2.1.0.

I opened an issue on this matter.


As of Angular 2.2.3 there is now a forwardRef() utility function that allows you to inject providers that have not yet been defined.

By not defined, I mean that the dependency injection map doesn't know the identifier. This is what happens during circular dependencies. You can have circular dependencies in Angular that are very difficult to untangle and see.

export class HeaderComponent {  mobileNav: boolean = false;  constructor(@Inject(forwardRef(() => MobileService)) public ms: MobileService) {    console.log(ms);  }}

Adding @Inject(forwardRef(() => MobileService)) to the parameter of the constructor in the original question's source code will fix the problem.

References

Angular 2 Manual: ForwardRef

Forward references in Angular 2