Advertisement
Guest User

Untitled

a guest
Mar 11th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnDestroy } from '@angular/core';
  2. import { ProjectService } from './_services/project.service';
  3. import { ActivatedRoute, Router, NavigationEnd } from '@angular/router';
  4. import { filter, map, switchMap } from 'rxjs/operators';
  5. import { Subscription } from 'rxjs';
  6.  
  7.  
  8. @Component({
  9.   selector: 'app-root',
  10.   templateUrl: './app.component.html',
  11.   styleUrls: ['./app.component.scss']
  12. })
  13. export class AppComponent implements OnDestroy {
  14.   private _projectIdSub: Subscription;
  15.   title = 'project-management';
  16.   constructor(
  17.     router: Router,
  18.     activatedRoute: ActivatedRoute,
  19.     private projectService: ProjectService) {
  20.     this._projectIdSub = router.events
  21.       .pipe(
  22.         filter((ev) => ev instanceof NavigationEnd),
  23.         map(() => {
  24.           let route = activatedRoute;
  25.           while (route.firstChild) {
  26.             route = route.firstChild;
  27.           }
  28.           return route;
  29.         }),
  30.         switchMap((r) => r.params),
  31.         map((prms) => prms.projectId && parseInt(prms.projectId, 10)),
  32.       ).subscribe((id) => {
  33.         this.projectService.currentProjectId = id;
  34.       });
  35.     activatedRoute.params.subscribe((p) => console.log(p));
  36.   }
  37.  
  38.   ngOnDestroy() {
  39.     this._projectIdSub.unsubscribe();
  40.   }
  41.  
  42.  
  43.   get projectId(): number {
  44.     return this.projectService.currentProjectId;
  45.   }
  46.  
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement