Shailesh Bhat
Shailesh Bhat

Reputation: 152

How to Go back to particular page by clearing routing history, using Angular Router/Location

Going back to the HOME Page by clearing the routing history is my requirement

I have tried with below code. But all codes shown below didn't met my requirement.

this.router.navigate(['/services/paymentProcessing'], { skipLocationChange: true });

ngOnDestroy(): void {
    window.history.go(-4);
    this.location.back();
    this.router.navigate(['/services/#SH']);
  }

I need to go back to Home Page/particular page from my specific page by clearing the routing history. Can anyone let me know the solution

Upvotes: 1

Views: 1672

Answers (1)

Shailesh Bhat
Shailesh Bhat

Reputation: 152

By referring to the answers at How to Detect Browser Back Button event - Cross Browser and How do I detect user navigating back in Angular2?, I wish to let you know the solution for above task.

For going back to particular page, by skipping current page you can use the code

this.router.navigate(["/C"], { replaceUrl: true });

as said by Mr. Stark Buttowski

And then if you wish to stay on current page(in my case HomePage) without clearing the history,

import { Location } from "@angular/common";

constructor(public location: Location){}

ngOnDestroy(): void {
    this.location.subscribe(
      x=> history.pushState(null, null, window.location.pathname)
    );
  } 

so that you can block back button navigation.

If there are, any better lines of code, looking forward..

Looking for the answer, which is better than above

Upvotes: 1

Related Questions