簡體   English   中英

Ionic 2:從根組件導航

[英]Ionic 2: Navigating from the Root Component

這是什么意思?

如果您想從根應用程序組件(用@App 而非@Page 裝飾的組件)控制導航怎么辦? 你不能注入 NavController因為導航控制器的任何組件都是根組件的子組件,所以它們不能被注入。

通過向 ion-nav 添加 id,您可以使用 IonicApp 服務獲取對 Nav 組件的引用,該組件是導航控制器(它擴展了 NavController):

以防萬一這是否可以幫助其他開發人員,並且在 Ionic2 beta 8 中

@App 和@Page 應替換為@Component。

現在您應該能夠通過執行以下操作從根組件導航:

import {Component, ViewChild, provide, PLATFORM_DIRECTIVES} from '@angular/core';
import {ionicBootstrap, App, Platform, MenuController, Nav} from 'ionic-angular';
import {StatusBar} from 'ionic-native';
import {HomePage} from './pages/home/home';

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

  rootPage: any = HomePage;

  constructor(private platform: Platform, private menu: MenuController) {
    this.initializeApp();
  }

  initializeApp(): void {
    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.
      StatusBar.styleDefault();
    });
  }

  openPage(aPage: any) {
    // close the menu when clicking a link from the menu
    this.menu.close();

    // navigate to the new page if it is not the current page
    this.nav.setRoot(aPage);
  }
}

ionicBootstrap(MyApp, [], {
  //config options
});

請記住,頁面和組件不是一回事。

你的頁面都導入了NavController,所以不需要破解DOM!! :) 您的當前頁面能夠在其自身之上推送任何其他頁面,只要您導入該頁面(當前頁面需要知道要推送的頁面的存在)。

this.nav.push(OtherPage);

類似地,當前頁面可以從視圖中彈出。

this.nav.pop();

它甚至可以直接更改根視圖。

this.nav.setRoot(OtherPage);

最后,這個在ionic2中導航的簡單指南將很好地解釋這一切......

要從根組件導航,首先您必須獲取該組件,然后再次設置它,請參見下面的示例:

this.app.getRootNav().setRoot(visitorsPage);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM