5

I have an issue with one service within my application. The issue is that wherever this service is being injected I'm getting the following error:

Uncaught (in promise): Error: Can't resolve all parameters for TestComponent: ([object Object], ?, [object Object])

I know that this issue could be caused because of the use of barrels however I'm not using barrels for services, but referencing the file "directly" for example: ../../core/services/config.service.

As suggested in this answer I tried to use @Inject(forwardRef(...)) wherever this service is being injected. The only exception I have is the AppComponent where in this case I'm injecting the same service "normally" and I get no error:

export class AppComponent implements OnInit {
    constructor(
        private configService: ConfigService, // NO ERROR
        private router: Router
    ) { }

    ...

}

In all the other places if I don't do something like the following I get the error described above:

export class BuyButtonComponent {
    constructor(
        @Inject(forwardRef(() => ConfigService)) private configService: ConfigService,
        // private configService: ConfigService // DOES NOT WORK
    ) { }

    ...

}

It is my understanding that forwardRef() is used when the dependency is to be injected but not yet defined. The ConfigService is provided from a CoreModule which is imported in the AppModule. The components are either in a SharedModule or a FeatureModule that I have.

Not sure if anyone can shed some light as to why this particular service requires the forwardRef in any component (bar the AppComponent), whilst all other services which are imported and provided in the same manner are injected the "normal way".

Update

Here's how the service is being provided:

CoreModule

@NgModule({
    providers: [
        // Other Services...
        ConfigService
    ]
})
export class CoreModule { }

AppModule

@NgModule({
    bootstrap: [AppComponent],
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        BrowserAnimationsModule,
        CoreModule,
        AppRoutingModule
    ]
})
export class AppModule { }

The ConfigService is provided from the CoreModule and no where else. Also the CoreModule is only imported in the AppModule. The idea is to have services provided from a single module. The service is being used in components that are declared in other modules (SharedModule and a FeatureModule).

Community
  • 1
  • 1
Daniel Grima
  • 2,765
  • 7
  • 34
  • 58
  • 3
    `forwardRef` is mostly used when an identifier needs to be used that is only declared further down within the same file. Your error message indicates that some dependency is not provided, or that there is a circular dependency. – Günter Zöchbauer Apr 08 '17 at 12:43
  • you most likely have circular dependency.. are you importing components in providers? for example is `BuyButtonComponent` imported in any provider? – Suraj Rao Apr 08 '17 at 12:49
  • What does the constructor of `TestComponent` look like? – Günter Zöchbauer Apr 08 '17 at 12:58
  • Thanks for your replies, I've updated the question with a sample of how the Services are provided. I'm not providing any services in any components. It could be a circular dependency but if it is so, I can't figure where it could be. The `TestComponent` has a constructor that injects two other services and the `ConfigService`: `constructor(private shoppingListService: ShoppingListService, private configService: ConfigService, private userService: UserService) { }` – Daniel Grima Apr 08 '17 at 13:00
  • Read [What is `forwardRef` in Angular and why we need it](https://medium.com/@maximus.koretskyi/what-is-forwardref-in-angular-and-why-we-need-it-6ecefb417d48) to understand why we need `forwardRef`. This is probably not your case – Max Koretskyi Jul 08 '17 at 16:24

0 Answers0