[HELP] NullInjectorError: No provider for HttpClient! having it correctly imported

When I try to use my service I get the following error:

Uncaught (in promise): NullInjectorError: R3InjectorError(HomePageModule)[ApiService -> ApiService -> HttpClient -> HttpClient -> HttpClient]: 
  NullInjectorError: No provider for HttpClient!

I have correctly imported the HttpModule in app.module.ts:

import { HttpClientModule } from '@angular/common/http';

  declarations: [AppComponent],
  entryComponents: [],
  imports: [HttpClientModule, BrowserModule, IonicModule.forRoot(), AppRoutingModule],
  providers: [{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy}],
  bootstrap: [AppComponent],

And my service is loaded in me main page constructor like this:

      public serv: ApiService,
    ){ }

What I’m doing wrong? Thanks in advance!

I’m using Ionic 6.17.1, on Windows 11

This is my service class code:

import { Injectable } from '@angular/core';
import {HttpClient, HttpClientModule, HttpHeaders, HttpErrorResponse} from '@Angular/common/http'
import { Observable, throwError } from 'rxjs';
import {retry, catchError} from 'rxjs/operators';

  providedIn: 'root'
export class ApiService {

  //API path
  constructor(private http: HttpClient) { }
1 Like

Case matters. If that’s really “@Angular”, it needs to be “@angular” instead. Incidentally, I recommend never using providedIn: 'root', especially for things like an API service. The reason is that it prevents easily mocking the service out during development and using a fake backend.

1 Like

A full-stack developer can assist you to fix this one. web design Birmingham