DEV Community

C.OG
C.OG

Posted on

Angular: Determine previous page URL

The getCurrentNavigation method of the angular router returns a property previousNavigation that has details on the previous navigation.

This can be used to determine the previous page url, or even if there was no previous navigation (initial load of a particular route).

It's worth noting that this method only works when used in the constructor of a components class.

export class SomeComponent  {

  constructor(
    private router: Router,
  ) {
  const previousNavigation = this.router.getCurrentNavigation().previousNavigation;

  const previousUrl = previousNavigation.finalUrl;
  }
}
Enter fullscreen mode Exit fullscreen mode

More on the Router.getCurrentNavigation() method

Top comments (0)