I am trying to implement a navbar that shows different links based on whether or not a user is logged-in.
I am using the angular2-jwt
library which provides the tokenNotExpired()
function.
I have 2 routes, /home
and /login
. I have a navbar component which is outside of the <router-outlet>
, which means it is initialized only once and not every time the route changes.
After successful login, I am invoking router.navigate(['/home])
. The home and login both have checks for if a user is logged-in in their respective ngOnInit()
functions. The home component is therefore able to detect the logged-in user.
However, I am unable to update the navbar since it is not informed of the login.
Could anyone please tell me the correct way to implement this change detection?
Thanks.
Expose login status via service
make your service singleton
user your service in your navbar
so you can change your template using service.
call
changeLoginStatus
function of the service when login status changes.