HttpClientModule Deprecation
The Http Client ModuleHttpClientModule has been deprecated as of Angular 18.AsSo, well.here's Angularwhat 17it introducedlooks standalonelike, components,to use Http in v18 and made NgModule optional.So, there are several changes included, here.forward.
HttpClient Migration
First. The HttpClientModule is deprecated.
So, we will now be injecting HttpClient into components, via provider.
Add this line to components that will need Http:
import { provideHttpClient } from '@angular/common/http';
Instead of the class constructor injector, your component/service will simply use the above import, and have a private variable with the injected HttpClient, like the below service example:
import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root' // Or use `providedIn: MyLibraryModule` if needed
})
export class MyHttpService {
private http = inject(HttpClient);
getData() {
return this.http.get('https://api.example.com/data');
}
}