I am trying to redirect the user to previous page when button is clicked following this link. I used the below line of code to get the previous route. When I print the prevURL on onInit method, it has value but when I tried to access the same value on button click it is empty. Is there any reason for it?
ngOnInit() {
this.router.events.pipe(filter((e: any) => e instanceof RoutesRecognized),
pairwise()
).subscribe((e: any) => {
this.previousUrl = e[0].urlAfterRedirects;
console.log(this.previousUrl);
});
}
BackToPrevRouteOnBtnClick(args) {
console.log(this.previousUrl);
}