DEV Community

loading...

Angular: Determine previous page URL

cogoo profile image Colin Ogoo ・1 min read

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

Discussion (0)

Forem Open with the Forem app