I am trying to make an Android app using Ionic framework. I implemented a side menu and when I try to push pages from the side menu, I get an error in the console saying:
我正在尝试使用Ionic框架制作Android应用程序。我实现了一个侧边菜单,当我尝试从侧面菜单中推送页面时,我在控制台中收到错误消息:
Cannot read property 'push' of undefined
无法读取undefined的属性'push'
app.ts
app.ts
import { Component, ViewChild } from '@angular/core';
import { ModalController, ionicBootstrap, Platform, MenuController, NavController } from 'ionic-angular';
import { StatusBar } from 'ionic-native';
import { HomePage } from './pages/home/home';
import {AppSettingsPage} from "./pages/app-settings/app-settings";
import {TabsPage} from "./pages/tabs/tabs";
import {ReserveRoomPage} from "./pages/reserve-room/reserve-room";
import {ConfirmedReservationsPage} from "./pages/confirmed-reservations/confirmed-reservations";
@Component({
templateUrl: 'build/app.html',
providers: [NavController]
})
export class MyApp {
@ViewChild('nav') nav: NavController;
private rootPage: any;
private pages: any[];
private icon = 'cog';
constructor(private platform: Platform, private menu: MenuController,
private modalCtrl: ModalController) {
this.menu = menu;
this.pages = [
{title: 'Home', component: HomePage, icon: 'home'},
{title: 'Settings', component: AppSettingsPage, icon: 'settings'},
{title: 'Reserve Room', component: ReserveRoomPage, icon: 'add'},
{title: 'My Reservations', component: ConfirmedReservationsPage, icon: 'book'},
];
this.rootPage = TabsPage;
platform.ready().then(() => {
StatusBar.styleDefault();
});
}
openPage(page){
this.menu.close();
this.nav.push(page.component);
}
}
ionicBootstrap(MyApp);
app.html
app.html
<ion-menu [content] = "content">
<ion-toolbar>
<ion-title><strong>Giman</strong>.lk</ion-title>
</ion-toolbar>
<ion-content>
<ion-list>
<button ion-item *ngFor="let p of pages" (click)="openPage(p)">
<ion-icon name="{{ p.icon }}"></ion-icon> {{ p.title }}
</button>
</ion-list>
</ion-content>
</ion-menu>
<ion-nav id="nav" [root]="rootPage" #content swipe-back-enabled="false"></ion-nav>
app-settings.ts (Example of a page I want to push)
app-settings.ts(我要推送的页面示例)
import { Component } from '@angular/core';
import { ViewController, NavController } from 'ionic-angular';
@Component({
templateUrl: 'build/pages/app-settings/app-settings.html',
})
export class AppSettingsPage {
constructor(private navCtrl: NavController, private view: ViewController) {
}
goBack(){
this.view.dismiss();
}
}
(I imported ModalController because I wanted to preview my App UI flow and because push didn't work, I used each page as a modal just for preview purposes. For the final app, I need push and pop)
(我导入了ModalController,因为我想预览我的App UI流程,因为push不起作用,我将每个页面用作模态仅用于预览目的。对于最终的应用程序,我需要push和pop)
2 个解决方案
#1
4
Set the @ViewChild
to NavController
but to Nav
.
将@ViewChild设置为NavController但设置为Nav。
import { Nav } from 'ionic-angular';
.....
@Component({
templateUrl: "build/app.html" //no need for the provider
})
class MyApp{
@ViewChild(Nav) nav:Nav; //remove the 'id' from the HTML, it will find the ion-nav tag
....
someFunction(){
this.nav.push(YourComponent);
//or
this.nav.setRoot(YourComponent);
}
}
#2
0
on IONIC 3, I used this and it works !! to call parent nav from childview
在IONIC 3上,我使用了它,它的工作原理!从子视图调用父导航
var navParent = this.navCtrl.parent.parent as NavController;
navParent.push('DetailsTabPage')
#1
4
Set the @ViewChild
to NavController
but to Nav
.
将@ViewChild设置为NavController但设置为Nav。
import { Nav } from 'ionic-angular';
.....
@Component({
templateUrl: "build/app.html" //no need for the provider
})
class MyApp{
@ViewChild(Nav) nav:Nav; //remove the 'id' from the HTML, it will find the ion-nav tag
....
someFunction(){
this.nav.push(YourComponent);
//or
this.nav.setRoot(YourComponent);
}
}
#2
0
on IONIC 3, I used this and it works !! to call parent nav from childview
在IONIC 3上,我使用了它,它的工作原理!从子视图调用父导航
var navParent = this.navCtrl.parent.parent as NavController;
navParent.push('DetailsTabPage')