【问题标题】:ion-menu not working ionic side menu is not showing离子菜单不起作用 离子侧菜单未显示
【发布时间】:2021-04-02 20:40:03
【问题描述】:

我正在尝试在 ionic 应用程序中添加侧边菜单,但它没有出现,我正在附加所有文件,请帮助我!!

app.html 文件

<ion-menu [content]="mycontent">
    <ion-header>
        <ion-toolbar>
            <ion-title>Menu</ion-title>
        </ion-toolbar>
    </ion-header>
    <ion-content>
        <ion-list>
            <button ion-item (click)=o nLoad(ServicesMessPage)>
                <ion-icon name="quote" item-left></ion-icon>
                Mess
            </button>
            <button ion-item (click)=o nLoad(ServicesLaundryPage)>
                <ion-icon name="quote" item-left></ion-icon>
                Laundry
            </button>
        </ion-list>
    </ion-content>
</ion-menu>
<ion-nav [root]="rootPage" #mycontent></ion-nav>

然后在 app.component.ts 我添加了所有必要的导入

app.component.ts 文件

import { Component, ViewChild } from '@angular/core';
import { Platform, NavController, MenuController } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { LoginPage } from '../pages/login/login';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage: any = LoginPage;
  @ViewChild('mycontent') nav: NavController
  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private menuCtrl: MenuController) {
    platform.ready().then(() => {
      statusBar.styleDefault();
      splashScreen.hide();
    });
  }

  onLoad(page: any) {
    this.nav.setRoot(page);
    this.menuCtrl.close();
  }
}

在 ServicesPage html 中我包含了菜单

services.html

<ion-header>
  <ion-navbar hideBackButton="true">
    <ion-buttons start>
      <button ion-button name="menu" menuToggle>
      </button>
    </ion-buttons>
    <ion-title>Dashboard</ion-title>
  </ion-navbar>

</ion-header>


<ion-content padding>

</ion-content>

【问题讨论】:

  • (click)=o nLoad(ServicesLaundryPage) 是打字错误吗?
  • 是的,这是拼写错误,但我仍然无法解决,它在另一个项目中工作正常
  • app.module.ts中有没有配置

标签: angular ionic3


【解决方案1】:

对于任何显示 ion-menu 的通用 *.html 文件,需要确保 4 件事:

  1. ion-menu 有 'contentId' 字段
  2. 上面的 'contentId' 作为 id 传递给同级 html 标记
  3. 同级 html 具有 ma​​in 标签和 class="ion-page"
  4. 在同级容器中使用 ion-menu-button 来切换菜单

给定代码,通过单击菜单按钮独立显示侧边菜单。

<ion-menu contentId="mainContent">
      <ion-header>
        <ion-toolbar>
          <ion-title>Menu</ion-title>
        </ion-toolbar>
      </ion-header>
       <!-- write your menu content here-->
 </ion-menu>     
 <div class="ion-page" id = "mainContent" main>
         <ion-buttons slot="start">
            <ion-menu-button></ion-menu-button>
         </ion-buttons>
             <!-- write your app content here-->
  </div>

注意:使用 ion-menu-button 是切换菜单屏幕的方法之一。您还可以使用 menuController 来更改菜单栏,如Here

所述

【讨论】:

  • 是的!!!整天都在为此苦苦挣扎……我所要做的就是添加“主要”属性,即使离子文档说它已被弃用。奇怪!!
【解决方案2】:

我正在我的项目上测试你的代码并且它运行良好,我唯一注意到的是你缺少按钮中的图标菜单

改变这一行

<button ion-button name="menu" menuToggle> </button>

services.html 中的这个

   <button ion-button menuToggle>
       <ion-icon name="menu"></ion-icon>
   </button>  

此行将显示菜单图标。

【讨论】:

  • 是的,这是拼写错误,但我仍然无法解决,它在另一个项目中工作正常
  • 你添加了我告诉你的那行,你仍然可以看到菜单按钮?
【解决方案3】:

首先你在app.html 中有错误,这可能是你的拼写错误。现在我重构你的代码如下:

<ion-menu [content]="mycontent">
    <ion-header>
        <ion-toolbar>
            <ion-title>Menu</ion-title>
        </ion-toolbar>
    </ion-header>
    <ion-content>
        <ion-list>
            <button ion-item (click)=onLoad(ServicesMessPage)>
                <ion-icon name="quote" item-left></ion-icon>
                Mess
            </button>
            <button ion-item (click)=onLoad(ServicesLaundryPage)>
                <ion-icon name="quote" item-left></ion-icon>
                Laundry
            </button>
        </ion-list>
    </ion-content>
</ion-menu>
<ion-nav [root]="rootPage" #mycontent></ion-nav>

还需要改在services.html

  <ion-navbar hideBackButton="true">
    <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>
    </button>
    <ion-title>Dashboard</ion-title>
  </ion-navbar>

【讨论】:

  • 是的,这是拼写错误,但我仍然无法解决,它在另一个项目中工作正常
【解决方案4】:

不要忘记将main 添加到您的ion-router-outlet。它应该看起来像:

&lt;ion-router-outlet main&gt;&lt;/ion-router-outlet&gt;

【讨论】:

    猜你喜欢
    • 2019-02-09
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 2014-10-14
    • 2021-03-24
    • 1970-01-01
    • 2017-12-09
    • 2019-12-09
    相关资源
    最近更新 更多