官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 鹤庆县| 安康市| 石狮市| 阳谷县| 海城市| 武威市| 凤台县| 甘洛县| 正定县| 宿松县| 南昌县| 嘉义市| 荆门市| 株洲县| 长顺县| 大理市| 瑞丽市| 佳木斯市| 泽普县| 玉树县| 台北市| 莎车县| 德阳市| 青冈县| 安吉县| 文水县| 同德县| 连云港市| 报价| 库伦旗| 永仁县| 大城县| 古交市| 庆元县| 浮山县| 常熟市| 安宁市| 临澧县| 营山县| 大理市| 汕头市|