wanghongsha 2020-03-26
import { HttpClient, HttpErrorResponse, HttpParams } from ‘@angular/common/http‘; @Injectable({ providedIn: ‘root‘ }) export class XXX { constructor( private readonly http: HttpClient) { } getXXX(): Observable<ModelClass> { return this.http.get<ModelClass>("url").pipe( catchError((err) => this.handleError(err))); } getYYY(page: number = 0, size: number = 10): Observable<Array<ModelClass>> { const params = new HttpParams() .set(‘page‘, String(page)) .set(‘size‘, String(size)); return this.http.get<any>("url", { params }).pipe( map(response => response.assetList as Array<ModelClass>), catchError(err => this.handleError(err, of([] as Array<ModelClass>))) ); } }