2
votes

Scratching my head on this error. 'Error: No provider for HttpParams!' Migrating from Http to HttpClient. Imported HttpClientModule in app.module.ts and made it a member in @NgModule -> imports

There is still an import for HttpModule in app.module.ts. Can it be clashing with the new HttpClientModule?

In my service I have:

import {HttpClient, HttpHeaders, HttpParams} from "@angular/common/http";

Can someone please enlighten me as to why this is happening? TIA, Pedro.

2
when is the error appearing? when you want to start the service or on a specific call? can you share your logs? - eLRuLL
Are you trying to inject HttpParams in your constructor? Perhaps you are injecting HttpParams instead of HttpClient - LLai
@LLai yes that's exactly it: in my service's constructor I have: private params: HttpParams that's what throwing the error. Btw I need a class's member for storing params to be available throughout the remaining logic - Pedro Doria Meunier
the params should be used like let params = new HttpParams().append('myParam', 'myValue'); - LLai
why are you trying to inject HttpParams? makes no sense - Jota.Toledo

2 Answers

3
votes

You are getting error for HttpClient so, you are missing HttpClientModule for that.

you should import it in "app.module.ts" file like this:

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

and mention it in the NgModule decorator like this:

 @NgModule({
   ...
   imports:[ HttpClientModule ]
   ...
 })

if this even doesn't work try clearing cookies of the browser and try restarting your server. Hopefully it may work, I was getting the same error.

2
votes

If you are using this [params: HttpParams] in your constructor, please remove it from the constructor.

import {HttpClient, HttpParams, HttpHeaders} from '@angular/common/http';
import {Injectable} from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class MarksService {
  constructor(private http: HttpClient, private params: HttpParams) {}
}