Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get active route data inside router.events

How do I get active route data like params inside the subscribed router.events?

I do not want to subscribe to this.route.params!

// Catch any events in a certain component, where I subscribe to it!
this.router.events.subscribe(event =>
{ 
// The event object has only the url (when is NavigationStart/End), there is nothing useful for me


});

UPDATE

My question is different because I do not ask how to react to router events!

I ask for how to get the activate route params within the router.events !

Thats a difference for sure!

like image 808
Pascal Avatar asked Jan 05 '17 23:01

Pascal


2 Answers

this.router.events.subscribe(event =>
{ 
  console.log(this.router.routerState.root);
  console.log(this.router.routerState.root.firstChild);
  console.log(this.router.routerState.root.firstChild && this.router.routerState.root.firstChild.firstChild);
});
like image 93
Günter Zöchbauer Avatar answered Sep 23 '22 05:09

Günter Zöchbauer


merge(of(this.route.snapshot.params), this.router.events.pipe(
      filter((event) => event instanceof NavigationEnd),
      map(() => this.route),
      map((route) => {
        while (route.firstChild) route = route.firstChild;
        return route;
      }),
      filter((route) => route.outlet === 'primary'),
      mergeMap(route => route.params),
    )).subscribe(p => {})
like image 21
robert king Avatar answered Sep 20 '22 05:09

robert king