0

I have two pages. One is /a and the other is /b.

There is the a tag in the A page. It is <a routerLink="b">B page</a>.

My requirement is that users cannot directly access page B. they must click the a link from page A to access page B. how should I write my routing guard.

user15163984
  • 459
  • 2
  • 8
  • You would probably have to use [CanActivate guards](https://angular.io/api/router/CanActivate) and check if the previous route was `/a` – MrCodingB Jun 04 '22 at 07:18
  • @MrCodingB Yes, but I can't get the information of the previous route – user15163984 Jun 04 '22 at 07:22
  • Well if there's no way to access the previous route with the `RouterStateSnapshot` and the `ActivatedRouteSnapshot`, you could subscribe to the activated routes or the router events in the guard and save the last route that way – MrCodingB Jun 04 '22 at 07:24

1 Answers1

1

I think you could setup a service that would listen to NavigationEnd events. By doing this you could store previously activated url.

Then in the guard you'd just fetch previous url and check if it's /a or not.

Take a look at this example implementation: https://stackoverflow.com/a/72112335/9932919

Kamil Chlebek
  • 159
  • 1
  • 6