HttpClientModule Deprecation
The HttpClientModule has been deprecated as of Angular 18.
So, here's what it looks like, to use Http in v18 and 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 { HttpClient } 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');
}
}
No Comments