This example demonstrates how to use Angular’s HttpClientModule.

Import HttpClientModule from app.module.ts:

Import {HttpClientModule} from ‘@angular/common/ HTTP’;

Add HttpClientModule to the AppModule @NgModule() imports array to register Angular’s HttpClient providers globally.

Register this HttpClientModule globally:

Now that the AppModule imports the HttpClientModule, the next step is to inject the HttpClient service into your service so your app can fetch data and interact with external APIs and resources.

The next step is to inject the HttpClient into the service:

Import {HttpClient} from ‘@angular/common/ HTTP’;

Note that the HttpClient imported from @angular/common/ HTTP in this step is different from the HttpClientModule imported from app.module.ts earlier.

Inject HttpClient into the Cart Service constructor:

In cart Service cart.service.ts, use HTTP to read the price data:

getShippingPrices() {
    return this.http.get('/assets/shipping.json');
  }
Copy the code

For more of Jerry’s original articles, please follow the public account “Wang Zixi “: