Typescript this.nav未定义-Ionic2
我对这段代码有问题:Typescript this.nav未定义-Ionic2,typescript,ionic-framework,ionic2,Typescript,Ionic Framework,Ionic2,我对这段代码有问题: import { Component, ViewChild } from '@angular/core'; import { Nav, NavParams } from 'ionic-angular'; import { Page1 } from '../page1/page1'; import { Page2 } from '../page2/page2'; import { Login } from '../auth/login/login'; @Component({
import { Component, ViewChild } from '@angular/core';
import { Nav, NavParams } from 'ionic-angular';
import { Page1 } from '../page1/page1';
import { Page2 } from '../page2/page2';
import { Login } from '../auth/login/login';
@Component({
templateUrl: 'home.html'
})
export class Home {
@ViewChild(Nav) nav: Nav;
rootHomePage: any = Page1;
pages: Array<{title: string, action: any}>;
user = this.navPrm.get('auth').current_user();
constructor(public navPrm: NavParams) {
console.log("TesteTesteTesteTesteTesteTesteTesteTesteTeste");
console.log(this.nav);
// used for an example of ngFor and navigation
this.pages = [
{ title: 'Page One', action: this.openPage(Page1) },
{ title: 'Page Two', action: this.openPage(Page2) },
{ title: 'Logout', action: this.user.logout() }
];
}
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);
}
}
从'@angular/core'导入{Component,ViewChild};
从“离子角度”导入{Nav,NavParams};
从“../Page1/Page1”导入{Page1}”;
从“../Page2/Page2”导入{Page2};
从“../auth/Login/Login”导入{Login};
@组成部分({
templateUrl:'home.html'
})
出口级家庭{
@ViewChild(导航)导航:导航;
rootHomePage:any=Page1;
页面:数组;
user=this.navPrm.get('auth').current_user();
构造函数(公共navPrm:NAVPRAMS){
日志(“teste”);
console.log(this.nav);
//用于ngFor和导航的示例
此页=[
{标题:“第一页”,操作:this.openPage(第1页)},
{标题:“第二页”,动作:this.openPage(第二页)},
{title:'Logout',操作:this.user.Logout()}
];
}
openPage(第页){
//将内容导航重置为仅包含此页面
//我们不希望在这种情况下显示“后退”按钮
this.nav.setRoot(第页);
}
}
因此,在openPage()方法中,this.nav(上面声明了nav)返回一个错误:this.nav未定义。有人知道为什么吗?试试
this.nav.rootNav.setRoot(第页)
。这应该行得通。它对我有用
还有一件事,不是user=this.navPrm.get('auth').current_user()当您从navParam
检索值时,code>应该位于构造函数内部。
(因为,我不熟悉您的代码,但对这部分不太清楚。只是看起来好像错了。)问题出在this.nav。。。由于它是未定义的,我无法访问Nav原型的任何方法…我也有点奇怪的用户声明,但它目前正在工作,所以。。。KKK试着进去看看。我知道这很难,但那可能是个问题。此外,如果没有,则需要显示整个代码。如您从何处设置此页面?这是应用程序的开始吗?此外,还显示home.html代码。此外,如果它不是应用程序的开始,请尝试使用构造函数(专用navCtrl:NavController)
,然后尝试this.navCtrl.setRoot(Page)
,如果它有效。另外,尝试删除@ViewChild,看看会发生什么。让我知道所有这些试验的结果是什么。我已经将navCtrl:NavController
添加到构造函数中,并将Nav
dependencie替换为NavController
,然后将this.navCtrl.setRoot(Page)
也许这个链接有助于找出空指针异常