2

Is there any way so that I can load different component on the same route(in my case the root route).

Basically I want to see if the user is logged in, if he is logged in, the dashboard component will be loaded, if is he not logged in, the home component will be loaded

And again just to make the question clear, I don't want to redirect the user to a DIFFERENT route using auth guards. I want to load different components on the same route

Manzur Khan
  • 2,366
  • 4
  • 23
  • 44
  • Take a look at Auth Guard: https://netbasal.com/implementing-auth-guard-with-componentless-route-in-angular-b50a21f3bd77 – P.S. Nov 30 '17 at 06:27
  • There are guards you can work with. Check this link https://blog.thoughtram.io/angular/2016/07/18/guards-in-angular-2.html. It gives a good understanding of guards. – Vinod Bhavnani Nov 30 '17 at 06:28
  • I have used guards and as far as I know guards as the name suggest can only guard the route whether to allow access or no, in case no access should be allowed we can redirect the user to a different route. But I don't want the user to be redirect to a different route in this case, but the root route itself should load different component based on my condition – Manzur Khan Nov 30 '17 at 06:31

1 Answers1

2

If you don't want to use guards and this is what you mentioned in your question, you can simply use ngIf.

Example:

<div *ngIf="this.authService.hasPermissionForDashboard()">
 <app-dashboard><app-dashboard>
</div>
<div *ngIf="this.authService.hasNotPermissionForDashboard()">
 <app-home><app-home>
</div>
G.Vitelli
  • 1,229
  • 9
  • 18
  • I was also thinking of this, but thought there must be some better way. Assuming there is no other "standard" way. I can get away with this. Thanks – Manzur Khan Nov 30 '17 at 06:47