Advertisement
andreadc

user-page.component.ts

Mar 3rd, 2020
253
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.  
  6. @Component({
  7.   selector: 'app-user-page',
  8.   templateUrl: './user-page.component.html',
  9.   styleUrls: ['./user-page.component.css']
  10. })
  11. export class UserPageComponent implements OnInit {
  12.  
  13.   user = new User;
  14.   userId:number = null;
  15.   subscriptionUserId;
  16.   subscriptionUser;
  17.  
  18.   constructor(private userService:UserService, private route: ActivatedRoute) {
  19.     this.subscriptionUserId = this.route.paramMap.subscribe((params : ParamMap)=> {  
  20.       this.userId = parseInt(params.get('userId'));
  21.     });
  22.   }
  23.  
  24.   ngOnInit() {
  25.  
  26.     this.subscriptionUser = this.userService.getUser(this.userId).subscribe( data => {
  27.      
  28.       this.user.firstName = data[0].firstname;
  29.       this.user.lastName = data[0].lastname;
  30.       this.user.username = data[0].username;
  31.      
  32.     }, error => {
  33.       console.error(error);
  34.     })
  35.   }
  36.  
  37.   ngOnDestroy(){
  38.     this.subscriptionUserId.unsubscribe();
  39.     this.subscriptionUser.unsubscribe();
  40.   }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement