淺談Angular HttpClient簡單入門
現(xiàn)代瀏覽器支持使用兩種不同的 API 發(fā)起 HTTP 請求:XMLHttpRequest 接口和 fetch() API。
@angular/common/http 中的 HttpClient 類為 Angular 應(yīng)用程序提供了一個簡化的 API 來實現(xiàn) HTTP 客戶端功能。
一、準備工作
首先在app.module.ts 導入 HttpClientModule。如下:
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
HttpClientModule,
]
})
export class AppModule {}

二、在需要引用HttpClient的service.ts中引入HttpClient,如下:
import { HttpClient } from '@angular/common/http';
export class ConfigService {
constructor(private http: HttpClient) { }
}

三、請求數(shù)據(jù)
return this.http.get/post(url:'請求地址' ,
options: {
headers: this.headers
})
.toPromise()
.then((data: any) => {
return data;
})
.catch((err) => {
console.log(err);
});
}

四、在對應(yīng)的component.ts文件中引入service

數(shù)據(jù)格式:
{
"lists":[
{"title":"","pic":""},
{"title":"","pic":""}
]
}
五、頁面上調(diào)用

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
angular同一頁面跳轉(zhuǎn)重新執(zhí)行的實現(xiàn)方法
這篇文章主要介紹了angular同一頁面跳轉(zhuǎn)重新執(zhí)行的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
AngularJS實踐之使用NgModelController進行數(shù)據(jù)綁定
大家都知道AngularJS中的指令是其尤為復雜的一個部分,但是這也是其比較好玩的地方。這篇文章我們就來說一說如何在我們自定義的指令中,利用ngModel的controller來做雙向數(shù)據(jù)綁定,本文對大家學習AngularJS具有一定的參考借鑒價值,有需要的朋友們可以參考借鑒。2016-10-10
Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件詳解
這篇文章主要給大家介紹了關(guān)于Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家具有一定參考學習價值,需要的朋友們下面來一起看看吧。2017-07-07
angular-ngSanitize模塊-$sanitize服務(wù)詳解
本篇文章主要介紹了angular-ngSanitize模塊-$sanitize服務(wù)詳解 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
Angular @HostBinding()和@HostListener()用法
本篇文章主要介紹了Angular @HostBinding()和@HostListener()用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03

