import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs/Observable' ; import { catchError } from 'rxjs/operators' ; import { HttpClient , HttpHeaders , HttpParams , HttpResponse , HttpEvent , HttpRequest } from '@angular/common/http' ; @ Injectable ( ) export class RandomUserService { randomUserUrl = 'https://api.randomuser.me/' ; getUsers ( PageIndex = 1 , pageSize = 10 , args ? ) { let options_ = { method : "get" , headers : new Headers ( { "Content-Type" : "application/json" , "Accept" : "application/json" } ) , params : this . parseParams ( { results : pageSize , page : PageIndex , _allow_anonymous : true } ) } ; return this . http . get ( this . randomUserUrl , options_ ) ; } handleError ( error ) { const errMsg = ( error . message ) ? error . message : error . status ? `${ error . status } - ${ error . statusText }` : 'Server error' ; console . error ( errMsg ) ; return Observable . throw ( errMsg ) ; } constructor ( private http ) { } parseParams ( params ) { let ret = new HttpParams ( ) ; for ( const key in params ) { let _data = params [ key ] ; ret = ret . set ( key , _data ) ; } return ret ; } }