Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Angular Router

You're reading from   Angular Router From Angular core team member and creator of the router

Arrow left icon
Product type Paperback
Published in Mar 2017
Publisher Packt
ISBN-13 9781787288904
Length 118 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Victor Savkin Victor Savkin
Author Profile Icon Victor Savkin
Victor Savkin
Arrow right icon
View More author details
Toc

Grouping by navigation ID

The router assigns a unique ID to every navigation, which we can use to correlate events. Perform the following steps.

  1. Let's start with defining a few helpers used for identifying the start and the end of a particular navigation:
    function isStart(e: Event): boolean {
      return e instanceof NavigationStart;
    }
    
    function isEnd(e: Event): boolean {
      return e instanceof NavigationEnd ||
             e instanceof NavigationCancel ||
             e instanceof NavigationError;
    }
    
  2. Next, let's define a combinator that will take an observable of all the events related to a navigation and reduce them into an array:
    function collectAllEventsForNavigation(obs: Observable):
      Observable<Event[]>{
      let observer: Observer<Event[]>;
      const events = [];
      const sub = obs.subscribe(e => {
        events.push(e);
        if (isEnd(e)) {
          observer.next(events);
          observer.complete();
        }
      });
      return new Observable<Event[]>(o => observer = o);
    }
    
  3. Now...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime