Search code examples
angularurl-routingangular-routingangular9angular-router

Angular 9 - get destination url when navigation starts


I'm using Angular 9. I need to know which will be the destination url as soon as the navigation starts. I'd like to do something like this:

constructor(router: Router) {
    this.router = router;
}

this.router.events.pipe(
    filter(event => event instanceOf NavigationStart),
    map(event => {
        //get somehow the destination url of this navigation just started
    })
).subscribe()

Is there a way to achive that only with the use of Angular Router?


Solution

  • you can use

    this.router.events.pipe(
      filter((e: Event): e is NavigationStart => e instanceof NavigationStart)
    ).subscribe((e: NavigationStart) => {
        e.url // contains the url you will go to
    });