Advertisement
andreadc

user-page.component.ts

Mar 4th, 2020
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2. import { UserService } from '../services/user.service';
  3. import { User } from '../models/user';
  4. import { ActivatedRoute, ParamMap } from '@angular/router';
  5. import { Observable, BehaviorSubject } from 'rxjs';
  6.  
  7. @Component({
  8.   selector: 'app-user-page',
  9.   templateUrl: './user-page.component.html',
  10.   styleUrls: ['./user-page.component.css']
  11. })
  12. export class UserPageComponent implements OnInit {
  13.  
  14.   user$: Observable<User>;
  15.   userId:number = null;
  16.   subscriptionUserId$;
  17.   subscriptionUser$;
  18.  
  19.   constructor(public userService:UserService, private route: ActivatedRoute) {
  20.     this.subscriptionUserId$ = this.route.paramMap.subscribe((params : ParamMap)=> {  
  21.       this.userId = parseInt(params.get('userId'));
  22.     });
  23.   }
  24.  
  25.   ngOnInit() {
  26.  
  27.     this.user$ = this.userService.getUserById(this.userId);
  28.      
  29.   }
  30.  
  31.   ngOnDestroy(){
  32.  
  33.     if(this.subscriptionUserId$)
  34.       this.subscriptionUserId$.unsubscribe();
  35.  
  36.     if(this.subscriptionUser$)
  37.       this.subscriptionUser$.unsubscribe();
  38.  
  39.   }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement