【问题标题】:Ionic - Menu Active State not changing on click离子 - 菜单活动状态在点击时不会改变
【发布时间】:2018-07-06 02:27:53
【问题描述】:

我正在使用 Ionic Side-Menu 应用程序。 上图突出了我的问题。我希望在导航到该特定页面时更改菜单活动状态。 菜单状态在单击菜单项进行导航时工作正常,而不是在其他任何地方进行导航。

从主页单击“转到列表”时,我在主页中使用以下代码。

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { ListPage } from '../list/list';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  listPage =  ListPage; 
  constructor(public navCtrl: NavController) {

  }

  goToList(){
    this.navCtrl.setRoot(this.listPage);
  }
}

app.component.ts

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

import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';
import { GalleryPage } from '../pages/gallery/gallery';

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

  rootPage: any = HomePage;
  activePage: any;

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

  constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen) {
    this.initializeApp();

    // used for an example of ngFor and navigation
    this.pages = [
      { title: 'Home', component: HomePage },
      { title: 'List', component: ListPage },
      { title: 'Gallery', component: GalleryPage}
    ];

    this.activePage = this.pages[0];

  }

  initializeApp() {
    this.platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      this.statusBar.styleDefault();
      this.splashScreen.hide();
    });
  }

  openPage(page) {
    // Reset the content nav to have just this page
    // we wouldn't want the back button to show in this scenario
    this.nav.setRoot(page.component);
    this.activePage = page;
  }

  checkActive(page){
    return page === this.activePage;
  }
}

有什么帮助吗?

【问题讨论】:

  • 能否也添加相应的HTML?或者更好地创建一个stackblitz

标签: navigation ionic3 side-menu


【解决方案1】:

我自己找到了答案,因此将其发布以供其他人将来参考。

ngAfterViewInit() 中添加了代码,它似乎可以工作。

ngAfterViewInit() 每次页面状态改变时都会被调用,view.instance.constructor.name 为你提供当前页面的构造函数名称,你可以简单地比较一下使用您的菜单数组找出您导航到的页面并将其设置为活动页面。

参考下面的完整代码

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

import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';
import { GalleryPage } from '../pages/gallery/gallery';

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

  rootPage: any = HomePage;
  activePage: any;

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

  constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen) {
    this.initializeApp();

    // used for an example of ngFor and navigation
    this.pages = [
      { title: 'Home', component: HomePage, active: false },
      { title: 'List', component: ListPage, active: false },
      { title: 'Gallery', component: GalleryPage, active: false}
    ];

    this.activePage = this.pages[0];

  }

  ngAfterViewInit(){
    this.nav.viewDidEnter.subscribe((view) => {
      for(let i = 0 ; i < this.pages.length ; i++){
        if(this.pages[i].component.name == view.instance.constructor.name){
           this.activePage = this.pages[i];
           break;
        }
      }
    });
  }

  initializeApp() {
    this.platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      this.statusBar.styleDefault();
      this.splashScreen.hide();
    });
  }

  openPage(page) {
    // Reset the content nav to have just this page
    // we wouldn't want the back button to show in this scenario
    this.nav.setRoot(page.component);
    this.activePage = page;
  }

  checkActive(page){
    // console.log(page.title, this.activePage.title, page === this.activePage);
    return page === this.activePage;
  }
}

【讨论】:

    猜你喜欢
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2018-03-14
    • 2014-09-02
    相关资源
    最近更新 更多