Angular HTTPClient的使用方法
這個例子演示了如何使用Angular的HttpClientModule.
在app.module.ts里導入HttpClientModule:
import { HttpClientModule } from ‘@angular/common/http’;
Add HttpClientModule to the AppModule @NgModule() imports array to register Angular’s HttpClient providers globally.
全局注冊這個HttpClientModule:
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.
下一步則是在service中注入該HttpClient:
import { HttpClient } from ‘@angular/common/http’;
注意這一步從@angular/common/http中導入的HttpClient和前面app.module.ts導入的HttpClientModule不一樣。
將HttpClient注入到Cart service的構造函數里:
在cart service cart.service.ts里 ,使用http讀取price數據:
getShippingPrices() {return this.http.get('/assets/shipping.json');}要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
總結
以上是生活随笔為你收集整理的Angular HTTPClient的使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何提取SAP UI5应用的技术信息
- 下一篇: MSYS以及MinGW安装