How to toggle Menu in Ionic 4

后端 未结 2 685
北恋
北恋 2020-12-31 03:44

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 t

相关标签:
2条回答
  • 2020-12-31 04:05

    in your app.component.html:

    <ion-app>
      <ion-split-pane when="sm">
          <ion-menu>
            <ion-header>
              <ion-toolbar color="primary" text-center>
                <ion-title>MyApp</ion-title>
              </ion-toolbar>
            </ion-header>
            <ion-content>
              <ion-list>
                <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-label>
                      {{p.title}}
                    </ion-label>
                  </ion-item>
                </ion-menu-toggle>
              </ion-list>
            </ion-content>
          </ion-menu>
          <ion-router-outlet main></ion-router-outlet>
      </ion-split-pane>
    </ion-app>
    
    0 讨论(0)
  • 2020-12-31 04:15

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

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

    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.
    
    @Component({
      selector: 'app-root',
      templateUrl: 'app.component.html'
    })
    export class AppComponent {
      constructor(
        public menuCtrl: MenuController
      ) {
        this.initializeApp();
      }
    
    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.

    0 讨论(0)
提交回复
热议问题