【问题标题】:ionic 3 navigation menu does not show upionic 3 导航菜单不显示
【发布时间】:2018-08-22 11:24:48
【问题描述】:

我对侧边菜单没有在不同情况下显示感到非常恼火和困惑。

我的应用流程和要求是: 最初,有一个带有 3 个选项卡且没有侧边菜单的登录页面。在第一个选项卡上,有产品,您可以将它们添加到购物车。将这些添加到购物车后,您可以单击购物车进行结帐。此时,如果用户尚未登录,则会出现一个模型弹出窗口,其中包含使用 facebook 登录选项。成功登录后,将显示添加到购物车的商品的订单摘要页面。由于用户现在已登录,因此应该会出现侧边菜单。

但是,菜单图标中发生的事情会显示出来,但在单击时,什么也没有发生。控制台等没有错误。

我已经验证,如果我不检查用户登录状态,那么登录页面上的菜单会正常显示。

相关代码:

app.html

<ion-menu [content]="content" type="overlay" style="opacity:0.95">
        <ion-header>
          <ion-toolbar>
            <ion-title>
            Menu
            </ion-title>
          </ion-toolbar>
        </ion-header>
        <ion-content style="background-color: #F5F5F6">
          <ion-grid no-padding>
              <ion-row style="padding-top:20px">
                  <ion-col text-center>
                      <button ion-button round (click)="doLogout()">Sign Out</button>
                  </ion-col>
              </ion-row>
          </ion-grid>
        </ion-content>
      </ion-menu>

<ion-nav [root]="rootPage" #content></ion-nav>

app.component.ts

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;

  rootPage:any = TabsPage;
}

home.html(第一个标签)

ion-header>
  <ion-navbar>
      <button *ngIf="core.user.loggedIn == true" ion-button menuToggle icon-only style="display: block !important;">
          <ion-icon name='menu'></ion-icon>
        </button>
    <ion-title>Cakes</ion-title>
    <ion-buttons end>
        <button *ngIf="getCartCount() > 0" ion-button (click)="openCart()"><ion-icon name="cart"></ion-icon>
        </button>
    </ion-buttons>
  </ion-navbar>
</ion-header>

home.ts

openCart(){
    console.log('start of open cart:' + this.core.user.loggedIn)
    if(this.core.user.loggedIn == false){
      //user not logged in so first show login screen
      console.log('take to login screen')
      let modal = this.modalCtrl.create(LoginPage);
      modal.present();
    }
  }

Login.ts

doLogin(){
    if (this.platform.is('cordova')) {
      return this.fb.login(['email', 'public_profile']).then(res => {
        const facebookCredential = firebase.auth.FacebookAuthProvider.credential(res.authResponse.accessToken);
         firebase.auth().signInWithCredential(facebookCredential);
         this.navCtrl.setRoot(OrderSummaryPage); 
      })
    }

  }

OrderSummaryPage.html

<ion-header>

  <ion-navbar>
      <button ion-button menuToggle icon-only style="display: block !important;">
          <ion-icon name='menu'></ion-icon>
        </button>
    <ion-title>Order Summary</ion-title>
  </ion-navbar>

</ion-header>

【问题讨论】:

  • OrderSummaryPage 被设置为 Root 页面而不是被推入堆栈?
  • 设置为root。 this.navCtrl.setRoot(OrderSummaryPage);
  • 你在哪里制作core.user.loggedIn = true
  • 好吧,在 login.ts 中进行身份验证时,有一个订阅方法,它执行 core.user = new User(user.displayName, user.photoURL, true)

标签: angular ionic-framework ionic3 navigation-drawer ion-navbar


【解决方案1】:

您的导航流程错误,您需要更改此流程,因为它的冲突菜单会从 OrderSummeryPage 中弹出。

不要将 OrderSummeryPage 设置为根页面,因为您的菜单实现不再有效并且不会显示在该页面上。

要解决此问题,您需要从主页推送OrderSummaryPage,那里有两个选项

  1. 隐藏后退按钮并显示菜单按钮。
  2. 不要在那里显示菜单按钮,只显示默认的后退按钮,当用户点击后退时,它会回到主屏幕,您将在此处获得菜单按钮。

通过单击菜单按钮,您将获得菜单屏幕。

检查此代码:

第 1 步:更新您的 OpenCart 方法:

openCart(){

    let loginModal = this.modalCtrl.create(LoginPage, { userId: 8675309 });
    loginModal.onDidDismiss(data => {
      console.log(data);
      this.navCtrl.push(OrderSummaryPage);
    });
    loginModal.present();
    
  }

第 2 步:在 LoginPage 中更新您的登录方法

login(){
    this.viewCtrl.dismiss()
}

现在,如果您想隐藏 OrderSummeryPage 上的后退按钮,请使用以下代码

<ion-navbar hideBackButton="true"> // for hiding back button.

希望你能理解以上的变化。

【讨论】:

    【解决方案2】:

    这样做:在你的 app.html 中

    <ion-menu [content]="content" id="login">
    <ion-header>
    <ion-toolbar>
      <ion-title>Menu</ion-title>
    </ion-toolbar>
    </ion-header>
    
    <ion-content>
    <ion-list>
      <button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)">
        <ion-icon [name]="p.icon" item-left></ion-icon>
        {{p.title}}
      </button>
    </ion-list>
    </ion-content>
    </ion-menu>
    
    <!-- Disable swipe-to-go-back because it's poor UX to combine STGB with     side menus -->
    <ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>  
    

    在你的 app.component.ts 的构造函数中

    this.initializeApp();
    
    // used for an example of ngFor and navigation
    this.pages = [
      { title: 'Home', component: HomePage, icon: "ios-home-outline" },
      { title: 'Page 1 title', component: Page1, icon: "ios-alert-outline" },
      { title: 'Page 2 title', component: Page2, icon: "ios-alert-outline" },
      { title: 'Page 3 title', component: Page3, icon: "ios-search-outline" },
      { title: 'Page 4 tile', component: Page4, icon: "ios-call-outline" },
      { title: 'Log Out', component: LogoutPage, icon: "ios-call-outline" }
    ];  
    

    在上面声明页面:

    pages: Array<{title: string, component: any, icon:any}>;  
    

    现在你想在哪里显示侧边菜单图标,在页面的构造函数中添加这个this.menuCtrl.enable(true, 'login');
    你不想在哪里显示菜单

    this.menuCtrl.enable(false, 'login');  
    

    对于你的问题,你可以试试这个

    if(loggIn == true){
      this.menuCtrl.enable(true, 'login');
    }else{
      this.menuCtrl.enable(false, 'login');
    }
    

    【讨论】:

    • 菜单已启用。尽管单击菜单图标时它不会显示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多