Advertisement
andreadc

user.service.ts

Mar 3rd, 2020
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpHeaders , HttpParams } from '@angular/common/http';
  3.  
  4. import { User } from '../models/user';
  5. import { tap, map, shareReplay, share, publishReplay, refCount } from 'rxjs/operators';
  6. import { Observable } from 'rxjs';
  7.  
  8. @Injectable({ providedIn: 'root' })
  9. export class UserService {
  10.    
  11.     headers = new HttpHeaders({
  12.         'Content-Type':  'application/json',
  13.         'Authorization': 'application/json'
  14.     });
  15.      
  16.     host = 'http://localhost:8080/';
  17.  
  18.     public user:Observable<User>;
  19.  
  20.     constructor(private http: HttpClient) { }
  21.  
  22.     getUserById(userId:number){
  23.         let params = new HttpParams().set('userId', String(userId));
  24.         let options = { headers: this.headers, params: params };
  25.  
  26.         return this.http.get(this.host + 'getUserById', options)
  27.             .pipe(
  28.                 map( response => response as User ),
  29.                 tap( user => console.table(user) )
  30.                
  31.             );
  32.     }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement