How can I get the previous route as a path in Angular?
I tried using a solution via this link, but it does not execute on first page load.
constructor(router: Router) {
router.events
.pipe(filter(event => event instanceof NavigationEnd))
.subscribe((event: NavigationEnd) => {
console.log('prev:', event.previousUrl);
this.previousUrl = event.url;
});
}
I used both RoutesRecognized
and NavigationEnd
, however they did not work.
I called this method in the constructor
and ngOnInit
, but neither of them execute on first page load.
Is there any method of obtaining the previous URL?
Thank you.