All files / src/app/nav/routing-history routing-history.service.ts

100% Statements 20/20
50% Branches 2/4
100% Functions 7/7
100% Lines 17/17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 351x 1x 1x 1x       1x 5x     1x 1x         5x     5x 5x 2x   2x       1x 3x 3x     1x  
import '../../rxjs-extensions';
import { Injectable, OnDestroy } from '@angular/core';
import { Router, Event as RouterEvent, NavigationStart, NavigationEnd, NavigationCancel, NavigationError } from '@angular/router';
import { filter } from 'rxjs/operators/filter';
import { Subscription } from 'rxjs/Subscription';
 
@Injectable()
export class RoutingHistory implements OnDestroy {
  public history: NavigationEnd[] = [];
  routerSubscription: Subscription;
 
  public get lastNavigationUrl() {
    return this.history.length
      ? `"${this.history[this.history.length - 1].url.replace(/%20/g, ' ')}"`
      : '';
  }
  constructor(
    private router: Router
  ) {}
 
  public loadRouting(): void {
    this.routerSubscription = this.router.events
      .pipe(filter(event => event instanceof NavigationEnd))
      .subscribe((navEnd: NavigationEnd) => {
        this.history = [...this.history, navEnd];
      });
  }
 
  ngOnDestroy() {
    Eif (this.routerSubscription) {
      this.routerSubscription.unsubscribe();
    }
  }
}