Angular自帶有http模塊能夠方便的進行Http請求。沒必要像Vue那樣安裝配置axios。html
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: 'app/app.component.html'
})
export class AppComponent {
constructor(private http: HttpClient) { }
ngOnInit() {
// 發起一個get請求
this.http.get('/api/people/1').subscribe(json => console.log(json));
}
}
複製代碼
注意:上面的this.http.get
... 處理HTTP最好放到單獨的Service文件中,再注入到Component。這裏爲了演示沒有這麼作。ios
有些時候咱們須要按順序發起多個請求,根據第一個請求返回的結果中的某些內容,做爲第二個請求的參數,好比下面代碼。json
ngOnInit() {
this.http.get('/api/people/1').subscribe(character => {
this.http.get(character.homeworld).subscribe(homeworld => {
character.homeworld = homeworld;
this.loadedCharacter = character;
});
});
}
複製代碼
上面的嵌套寫法可讀性不那麼好,咱們能夠使用RxJS提供的mergeMap
操做符來優化上述代碼axios
import { Component } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { mergeMap } from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: 'app/app.component.html'
})
export class AppComponent {
homeworld: Observable<{}>;
constructor(private http: HttpClient) { }
ngOnInit() {
this.homeworld = this.http.get('/api/people/1')
.pipe(
mergeMap(character => this.http.get(character.homeworld))
);
}
}
複製代碼
mergeMap 操做符用於從內部的 Observable 對象中獲取值,而後返回給父級流對象。 能夠合併 Observable 對象segmentfault
forkJoin 是 Rx 版本的 Promise.all(),即表示等到全部的 Observable 都完成後,才一次性返回值。api
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { forkJoin } from "rxjs/observable/forkJoin";
@Component({
selector: 'app-root',
templateUrl: 'app/app.component.html'
})
export class AppComponent {
loadedCharacter: {};
constructor(private http: HttpClient) { }
ngOnInit() {
let character = this.http.get('https://swapi.co/api/people/1');
let characterHomeworld = this.http.get('http://swapi.co/api/planets/1');
forkJoin([character, characterHomeworld]).subscribe(results => {
// results[0] is our character
// results[1] is our character homeworld
results[0].homeworld = results[1];
this.loadedCharacter = results[0];
});
}
}
複製代碼
在線演示bash
使用 catchError 處理observable中的錯誤,須要返回一個新的 observable 或者直接拋出error併發
例1 ,在請求方法內部處理錯誤,若請求失敗返回一個默認值,看起來用戶也感知不到發生了錯誤app
// http.service.ts
getPostDetail(id) {
return this.http
.get<any>(`https://jsonplaceholder.typicode.com/posts/${id}`)
.pipe(
// catchError 須要 returning a new observable or throwing an error.
catchError(err => {
// 若是發生錯誤,用缺省值,(嘗試修改成錯誤地址)
return of({
userId: 1,
id: 1,
title: '-occaecati excepturi optio reprehenderit-',
body: '-eveniet architecto-'
});
})
)
}
// component 中調用
getPostDetail() {
this.postDetail$ = this.service.getPostDetail(1)
.subscribe(val => {
console.log(val);
});
}
複製代碼
例2 直接把錯誤拋出來,在外部處理錯誤,好比來個彈窗,提示告訴用戶post
getPostDetail(id) {
return this.http
.get<any>(`${this.endpoint}/posts2/${id}`)
.pipe(
// catchError returning a new observable or throwing an error.
catchError(err => {
throw err;
})
)
}
// 改造調用方法
getPostDetail() {
this.postDetail$ = this.service.getPostDetail(1)
.subscribe(
(next) => {
},
// 這裏接收內部拋出的錯誤
err => {
// 能夠加入本身的錯誤處理邏輯,搞個彈窗,notify等
console.log(err);
}
)
}
複製代碼