my App structure is the following (app-component):
<app-navbar></app-navbar>
<router-outlet></router-outlet>
<app-footer></app-footer>
In the navbar I want to display the user's name, the problem is that in order to do that, there are two ways:
In the navbar component I can read the current logged user
onInit
, get the name and display it. This way works just fine with the exception that it won't work correctly until you reload the page (or call navbar'sonInit
in order to read the user and get the value). If done this way, I would need to call navbar'sonInit
through login component (which is inside the<router-outlet>
) after all data has been set.The other way is following this post (section 3. Siblings) which would mean to make an output in login component sending the user's name (this works so far), receiving it in app component (through the router-outlet tag) and sending the received data through an input in navbar component. I've also tried this, but app component won't receive the data (I'm guessing due to being inside a router-outlet).
<app-navbar [nombreNav]="nombreParent"></app-navbar>
<router-outlet (nombreLogin)="recibeNombre($event)"></router-outlet>
<app-footer></app-footer>
(nombreLogin is the Output EventEmitter in login component; recibeNombre() is the method in app component which should get the name and set it's value to nombreParent, which will be inputted to navbar component. I'm not pasting the code because it's literally that and it's the same as in the post)
So I would need either a way of calling navbar's onInit
(which I've seen is possible with the use of outputs the same way as option 2) or send data through the <router-outlet>
. How could I solve this?
Thanks.