0

in app.component I need to perform an action based on the ActivatedRoute. using: Angular6, rxjs6 (with it's new syntax)

import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';

....

constructor (private route:ActivatedRoute){
  const url: Observable<string> =route.url.pipe(map(segments=>segments.join('')));

}

the intent is to show/hide something based on changes in the activated route.

Thank you in advance!

Keslavi
  • 143
  • 3
  • 14

1 Answers1

2

You can subscribe to the route like any normal observables, then put your logic inside the subscribe.

import { Router } from '@angular/router';

....

constructor (private router: Router) {
  this.router.events.subscribe((event) => {
      console.log(event);
  });
}

Docs

bugs
  • 14,631
  • 5
  • 48
  • 52
  • I may be doing it wrong. Did it in the constructor, it only runs once. i need to retrieve for each change: route.url.subscribe(data=>console.log(data);}) returns it only the first time – Keslavi May 17 '18 at 16:17
  • Modified the answer – bugs May 17 '18 at 16:22
  • looks pretty good, unsure how to isolate this to NavigationEnd though. the event doesn't define that in a name. – Keslavi May 17 '18 at 18:53