0

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);
}
LilRazi
  • 690
  • 12
  • 33

1 Answers1

1

you can navigate user by

  1. this.router.navigate(['../'])
  2. window.history.back()
  3. window.history.go(-1) But I recommend to you usage of #1

Is this working for you?

Ashot Aleqsanyan
  • 4,252
  • 1
  • 15
  • 27