Last active
November 30, 2019 09:39
-
-
Save NishuGoel/06024c31888caaaa799fc0da3f595b13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import {HttpClient, HttpHeaders} from '@angular/common/http'; | |
import {Observable, throwError} from 'rxjs'; | |
import { tap, catchError } from 'rxjs/operators'; | |
import {User} from './user-data'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class AppService { | |
apiurl = 'api/users'; | |
headers = new HttpHeaders().set('Content-Type', 'application/json').set('Accept', 'application/json'); | |
httpOptions = { | |
headers: this.headers | |
}; | |
constructor(private http: HttpClient) { } | |
private handleError(error: any) { | |
console.error(error); | |
return throwError(error); | |
} | |
getUsers(): Observable<User[]> { | |
return this.http.get<User[]>(this.apiurl).pipe( | |
tap(data => console.log(data)), | |
catchError(this.handleError) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment