前端開發,axios是標配的http請求發起libary, 採用的是Promise的方式。而後,Angular中採用的是另一種形式Observable,觀察訂閱模式。Angular默認推薦採用內置的HTTPClient。 下面讓咱們開始今天的主題,HTTPClient前端
import {HttpClientModule} from '@angular/common/http';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
}
複製代碼
this.http.get<Config>(this.configUrl)
.subscribe((data: Config) => this.config = { ...data });
複製代碼
this.http.get<Config>()...
複製代碼
this.http.get(this.configUrl)
.subscribe((data: any) => this.config = { ...data });
複製代碼
等效於ios
this.http.get<Object>(this.configUrl)
.subscribe((data: Object) => this.config = { ...data });
複製代碼
「問題1: 若是服務端,返回的數據就是一個text文本,譬如Hello,world,你猜會怎麼樣?」web
# 必須.鏈式set,不然參數空
const params = new HttpParams()
.set('orderBy', '"$key"')
.set('limitToFirst', "1");
this.http.get(this.configUrl,{params})
.subscribe((data: any) => this.config = { ...data });
複製代碼
編程
const params = new HttpParams({ fromString: 'orderBy="$key"&limitToFirst=1' }); 複製代碼this.http.get(this.configUrl,{params}) .subscribe((data: any) => this.config = { ...data }); 複製代碼
「問題2: 若是前端想拿到後端api header頭中參數,怎麼辦?」json
this.http.post(url,
{
"courseListIcon": "...",
"description": "TEST",
"iconUrl": "..",
"longDescription": "...",
"url": "new-url"
})
.subscribe(
(res) => {
console.log("POST call successful value returned in body",
res);
},
error => {
console.log("POST call in error", error);
},
() => {
console.log("The POST observable is now completed.");
});
}
複製代碼
this.http.delete(url1)
.subscribe(
(res) => {
console.log("DELETE call successful value returned in body",
res);
},
error => {
console.log("DELETE call in error", error);
},
() => {
console.log("The DELETE observable is now completed.");
});
}
複製代碼
this.http.patch(url,
{
"description": "Angular Tutorial For Beginners PATCH TEST",
})
.subscribe(
(res) => {
console.log("PATCH call successful value returned in body",
res);
},
error => {
console.log("PATCH call in error", error);
},
() => {
console.log("The PATCH observable is now completed.");
});
}
複製代碼
const params = new HttpParams({
fromString: 'orderBy="$key"&limitToFirst=1'
});
this.http.request("GET",this.configUrl, { params })
複製代碼
const headers = new HttpHeaders()
.set("X-CustomHeader", "custom header value");
this.http.get(this.configUrl,{ headers })
.do(console.log)
.map(data => _.values(data));
複製代碼
const headers = {
"X-CustomHeader", "custom header value",
'content-type': 'application/json'
}
this.http.get(this.configUrl,{ headers })
.do(console.log)
.map(data => _.values(data));
複製代碼
get(url: string, options?: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
# 默認值有: response| body| event
observe?: 'body';# 默認讀取的是response中的body
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
# 默認值有: arraybuffer | json | blob |text
responseType?: 'json';# 這裏,ts參數類型可選,默認值json,
withCredentials?: boolean;
}): Observable<Object>;
複製代碼
this.http.get(this.configUrl,{responseType:'text'})
.subscribe((data: any) => this.config = { ...data });
複製代碼
this.http.get<Config>(
this.configUrl, { observe: 'response' })
.subscribe((data: any) => this.config = { ...data });
複製代碼
const request = new HttpRequest(
"POST", this.uploadURL, {},{observe: 'events',reportProgress: true});
this.http.request(request)
.subscribe(
event => {
# 文件上傳進度斷定
if (event.type === HttpEventType.DownloadProgress) {
console.log("Download progress event", event);
}
if (event.type === HttpEventType.UploadProgress) {
console.log("Upload progress event", event);
}
if (event.type === HttpEventType.Response) {
console.log("response received...", event.body);
}
}
);
複製代碼
const parallel$ = Observable.forkJoin(
this.http.get(url1),
this.http.get(url2)
);
parallel$.subscribe(
values => {
console.log("all values", values)
}
);
複製代碼
const sequence$ = this.http.get<Config>(url1)
.switchMap(config => {
config.description+= ' - TEST ';
return this.http.put(url2,config)
});
sequence$.subscribe(
values => console.log("result observable ", values)
);
複製代碼
this.http
.get("/api/simulate-error")
.catch( error => {
// here we can show an error message to the user,
// for example via a service
console.error("error catched", error);
return Observable.of({description: "Error Value Emitted"});
})
.subscribe(
val => console.log('Value emitted successfully', val),
error => {
console.error("This line is never called ",error);
},
() => console.log("HTTP Observable completed...")
);
複製代碼
import {Injectable} from "@angular/core";
import {HttpEvent, HttpHandler, HttpInterceptor}
from "@angular/common/http";
import {HttpRequest} from "@angular/common/http";
import {Observable} from "rxjs/Observable";
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private authService: AuthService) {
}
intercept(req: HttpRequest<any>,
next: HttpHandler):Observable<HttpEvent<any>> {
const clonedRequest = req.clone({
headers: req.headers.set(
'X-CustomAuthHeader',
authService.getToken())
});
console.log("new headers", clonedRequest.headers.keys());
return next.handle(clonedRequest);
}
}
複製代碼
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [
[
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true
}
]
],
bootstrap: [AppComponent]
})
export class AppModule {
}
複製代碼
angular bootstrap
angular 官網 axios