import { HttpClient } from '@angular/common/http'; constructor(private http: HttpClient) {} ngOnInit() { this.http.get('https://jsonplaceholder.typicode.com/posts/1').subscribe( (response) => { console.log(response); }, (error) => { console.error(error); } ); }
import { HttpClient, HttpParams } from '@angular/common/http'; constructor(private http: HttpClient) {} ngOnInit() { let params = new HttpParams().set('userId', '1'); this.http.get('https://jsonplaceholder.typicode.com/posts', { params }).subscribe( (response) => { console.log(response); }, (error) => { console.error(error); } ); }In this example, we import the HttpParams class along with the HttpClient service. We create a new instance of HttpParams and set a query parameter 'userId' to '1'. We then pass this HttpParams instance as an option object to the `get` method along with the endpoint URL. This sends a GET request to https://jsonplaceholder.typicode.com/posts?userId=1. Overall, the `@angular/common/http` package library is used to perform HTTP requests in Angular applications using the HttpClient service. The `get` method is one of the most commonly used methods for retrieving data from a server.