6

How could i restrict a component to load on back press. Actually i have three component A(Normal Component), B(LoginComponent) and C(Otp Verification Component). I'm routing to component B from A on some button click and from B to C to verify user. now after verification done on component C, i want to go back on component 'A', and want to remove component 'B' and 'C' from history so that on back press from browser it does not load 'B'(LoginComponent) and 'C' again. so how could i do this in angularjs 2.

Ravi Sevta
  • 2,817
  • 21
  • 37
  • I suppose you could use route guards. [RouteGuards](https://angular.io/docs/ts/latest/guide/router.html#!#guards) – Mike Lunn Nov 19 '16 at 00:54
  • They allow you to hook into the component lifecycle giving you the ability to cancel navigation. – Mike Lunn Nov 19 '16 at 01:01

1 Answers1

15

While navigating from components B and C, pass the option replaceUrl:true to replace B with C in the browser history.

Eg:

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

Checkout the documentation for replaceUrl

Vandesh
  • 6,368
  • 1
  • 26
  • 38