#Call ngOnInit from another unrelated component

3 messages · Page 1 of 1 (latest)

tall sequoia
#

I have navbar component & sign in form component.
I am trying to call the ngOnInit of the navbar, once I click sign in, in my sign in form component.
basically to check wether a user is logged in or not.

lost oriole
#

The solution is wrong in 3 ways: lifecycle hooks are meant to act upon lifecycle of a component. Here calling ngOnInit is not meant to affect navbar.
Secondly ngOnInit's block is just pure JavaScript. If you need to call what ngOnInit computes, move the logic to a dedicated function.
Finally the architecture is wrong. That's not a navbar responsibility to be the gatekeeper about the user's current status.
Such a logic should live in a service and both your navbar and your form components should use it directly.

tall sequoia