I have a menu that is displayed occupying 100% on small screens, each menu item has a [routerLink] that works perfectly, the problem is that when clicking on the items , the view is shown but the menu continues to occupy 100% screen, and I want it to disappear or hide when the view loads .
When displaying the menu:
When clicking on any item (as you can see the login view is shown)
This is the code:
<nav id="menu" class="menu">
<a [routerLink]="'/home'"><i class="fa fa-home mr-2" aria-hidden="true"></i>Home</a>
<a [routerLink]="'/contact/contact'"><i class="fa fa-envelope mr-2" aria-hidden="true"></i>Contacto</a>
<a [routerLink]="'auth/login'"><i class="fa fa-user mr-2" aria-hidden="true"></i>Login</a>
</nav>
.menu{
position: absolute;
top: 50px;
left: 0;
width: 100%;
height: 100vh;
background: rgb(41, 50, 64, 0.8);
transition: all 0.5s;
transform: translateX(-100%);
}
// clase del boton que despliega el menu
#menu-bar:checked ~ .menu{
transform: translateX(0%);
}
what I tried to do was: link a hideMenu() function to each item , with this function I try from typescript to hide the menu by adding a "hideMenu" class and the corresponding router.navigate depending on the clicked item. The router.navigate works, but the menu is not hidden.
<nav id="menu" class="menu">
<a (click)="hideMenu('home')"><i class="fa fa-home mr-2" aria-hidden="true"></i>Home</a>
<a (click)="hideMenu('contact')"><i class="fa fa-envelope mr-2" aria-hidden="true"></i>Contacto</a>
<a (click)="hideMenu('login')"><i class="fa fa-user mr-2" aria-hidden="true"></i>Login</a>
</nav>
from typescript
hideMenu(name:string){
console.log(name);
const element = document.getElementById("menu");
element.classList.add("hideMenu");
if(name === 'home'){
this.router.navigate(['/home']);
}
if(name === 'contact'){
this.router.navigate(['/contact']);
}
if(name === 'login'){
this.router.navigate(['/auth/login']);
}
}
Here the hideMenu class
.hideMenu{
.menu{
transition: all 0.5s;
transform: translateX(-100%);
}
}
I solved it this way:
In the nav/menu I use [NgClass] where I add 2 classes to it , one class to hide the menu and the other to show it . For each menu item including the toggle button I include the hideMenu() function in the (click) event .
component.html
component.sass/css
In typescript component.ts