Inject dependencies

In order to make API calls, you will be leveraging the HttpClient module in Angular. The official documentation (https://angular.io/guide/http) explains the benefits of this module succinctly:

"With HttpClient, @angular/common/http provides a simplified API for HTTP functionality for use with Angular applications, building on top of the XMLHttpRequest interface exposed by browsers. Additional benefits of HttpClient include testability support, strong typing of request and response objects, request and response interceptor support, and better error handling via APIs based on Observables."

Let's start with importing the HttpClientModule in to our app, so we can inject the HttpClient within the module into the WeatherService:

  1. Add HttpClientModule to app.module.ts, as follows:
src/app/app.module.ts
...
import { HttpClientModule } from '@angular/common/http'
...
@NgModule({
  ...
  imports: [
    ...
    HttpClientModule,
    ...
  1. Inject HttpClient provided by the HttpClientModule in the  WeatherServiceas follows:
src/app/weather/weather.service.ts
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'

@Injectable()
export class WeatherService {
constructor(private httpClient: HttpClient) {}
}

Now, httpClient is ready for use in your service.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.0.145