I'm new at angular. Basically, my project is composed of several components. the first one is the loggin, where the menu is not shown, in the rest of the components yes (if the user has logged in). How to know if the user has logged in? it's easy, because when the login component logs in, it is stored in localstorage "iduser".
the logic that you will see next I am doing it from my app.component.ts.
import { Component,EventEmitter } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
showMenuEmitter = new EventEmitter<boolean>();
showMenu: boolean;
constructor(
private router : Router
){
console.log(this.router.url);
if(!localStorage.getItem("idusuario")){
this.showMenuEmitter.emit(true);
this.router.navigate(["/"]);
}
else if(localStorage.getItem("idusuario") && this.router.url=="/"){
this.router.navigate(["avance_entregables"]);
this.showMenuEmitter.emit(false);
console.log( this.showMenu );
}
this.showMenuEmitter.subscribe(
mostrar => this.showMenu = mostrar
);
}
}
Here I am trying to establish whether or not to show the menu. and in app.component.html I would like to hide it or show it.
<menu *ngIf="showMenu"></menu>
<router-outlet></router-outlet>
I have never used EventEmitter, but I have looked for examples and apparently it would be the best practice. What am I doing wrong?