I was attempting to create an observable that could be used to conditionally hide the main navigation menu in my app - so, when the current route was the login page, the navigation would be hidden, otherwise it would be shown.
However, i couldn't figure out a simple way to do this. I know that you could inject the ActivatedRoute, which contains observables to monitor the params and the currently activated URL segment, but the observable I need is required on the AppComponent, which sits outside the router outlet.
My eventual solution was to monitor the router itself for navigation events and compose my required observable manually:
/** Returns a boolean observable that indicates, for each route,
* if the menu should be displayed or not
*/
isMenuVisible$(): Observable<boolean>{
let isMenuCurrentlyVisible = !this.router.isActive('login', true);
return this.router.events
.filter(e => e instanceof NavigationEnd) //every route change fires multiple events - only care about the route change as a whole
.map(() => !this.router.isActive('login', true))
.startWith(isMenuCurrentlyVisible)
;
}
However, this use case doesn't seem that strange, so i was wondering if there was an easier way of accomplishing what i wanted?