1

In my Angular 2 (final) application, I need to get the router name from which I navigated from.

For Example: If I navigate from this url

'http://localhost:3000/#/home' to 'http://localhost:3000/#/about-us'

(i.e) From home component to about us component.

Now, my problem is how to get the route name of component, in short how to get the route name from where I navigated from.

Thanks in advance.

AT82
  • 71,416
  • 24
  • 140
  • 167
Manush
  • 1,852
  • 7
  • 26
  • 39

1 Answers1

0

You need to subscribe to route changes event in both parent & child routes and keep the currentUrl, to use it when the route changes next time.


    previousUrl: string;
    constructor(router: Router) {
      router.events
      .filter(event => event instanceof NavigationEnd)
      .subscribe(e => {
          this.previousUrl = e.url;
      });
    }

Please see this answer & How to detect a route change in Angular 2?

Alok Jha
  • 353
  • 3
  • 9