How close menu from ionic 5

Viewed 996

I have a simple question but I cant't find a solution. I created a new project in Ionic 5 and I use "sidemenu" template. From documentation I read that close method should close the menu. I put in the constructor "public menuCtrl: MenuController" and in ngOnInit:

this.menuCtrl.close();

but nothing works. From debug I see menuCtrl empty.... Any ideas?

1 Answers

The MenuController is in your top level app.component page not in the individual page so you would need to write the code inside app.component.ts.

Alternatively you can automate it by wrapping each menu item in a ion-menu-toggle like:

  <ion-list>
    <ion-menu-toggle auto-hide="true" *ngFor="let page of navigate">
      <ion-item [routerLink]="page.url" routerDirection="forward">
        <ion-icon [name]="page.icon" slot="start"></ion-icon>
        {{ page.title }}
      </ion-item>
    </ion-menu-toggle>
  </ion-list>
Related