Home > Back-end >  Need injection token for something provided in root
Need injection token for something provided in root

Time:02-07

I have a service that is used throughout my app to do admin functions. Normally all I have to do to use it is add it to the list of parameters of a constructor.

like so

constructor(private adminService: AdminService) { }

Which is exactly what is shown in the angular.io documentation

However I needed to use it a bit earlier on, like right after authentication and I am getting a weird error that I have no idea how to resolved based on the documentation.

The error message

 No suitable injection token for parameter 'adminService' of class 'LandingLayoutComponent'

admin.service.ts

    @Injectable({
      providedIn: 'root'
    })
    export class AdminService {}
    constructor(private httpService: ApplicationHttpClient) { }

app.module.ts

@NgModule({
  declarations: [
    AppComponent,
    LandingLayoutComponent,
  ],
  imports: [],
  providers: [
    ApplicationHttpClient,
  ],
  bootstrap: [AppComponent]
})

Update, shared module does it differently.

providers: [
  {
    provide: ApplicationHttpClient,
    useFactory: applicationHttpClientCreator,
    deps: [HttpClient, AuthenticationService, ProgressBarService]
  }],

CodePudding user response:

I'm curious as to what your ApplicationHttpClient looks like. Does it use the HttpClient class? If so, then you probably should also include the HttpClientModule in your Application Module.

  •  Tags:  
  • Related