How to toggle Menu in Ionic 4

大兔子大兔子 提交于 2019-12-04 19:30:56


I am new to Ionic, and I am developing a basic app in Ionic 4 with sidemenu.

I am adding a button in Side Menu, when I am clicking on that button the menu is not toggling. can anyone help me in this ? here's the code which I am trying.

<ion-button color="primary" (click)="function()" class="class" menuToggle expand="block" fill="outline"> text </ion-button>

Explanation of not Duplicate question

Please check the Bold text, I already have a working sidemenu, but the problem is I want to close the sidemenu when I click on ion-button, not on ion-item.


Simply encapsulate your ion-button within an ion-menu-toggle element, like so:

  <ion-button>Toggle Menu</ion-button>

View the documentation here

EDIT: If you don't want to use ion-menu-toggle, you can do this instead:

In your app.component.ts:

import { MenuController } from '@ionic/angular'; //import MenuController to access toggle() method.

  selector: 'app-root',
  templateUrl: 'app.component.html'
export class AppComponent {
    public menuCtrl: MenuController
  ) {

toggleMenu() {
    this.menuCtrl.toggle(); //Add this method to your button click function


And in your app.component.html:

<ion-button (click)="toggleMenu()">Toggle Menu</ion-button>

To view all methods, check the docs here.


in your app.component.html:

  <ion-split-pane when="sm">
          <ion-toolbar color="primary" text-center>
            <ion-menu-toggle auto-hide="false" *ngFor="let p of pages">
              <ion-item [routerDirection]="'root'" href={{p.url}}>
                <ion-icon slot="start" [name]="p.icon"></ion-icon>
      <ion-router-outlet main></ion-router-outlet>