[英]this.nav is undefined - 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({
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);
}
}
因此,在 openPage() 方法中,this.nav(nav 在上面聲明)返回錯誤:this.nav 未定義。 有人知道為什么嗎?
試試this.nav.rootNav.setRoot(page)
。 這應該有效。 這個對我有用。
還有一件事,不是user = this.navPrm.get('auth').current_user();
當您從navParam
檢索值時,應該在構造函數內。 (因為,我不熟悉你的代碼,但不確定這部分。似乎錯了。)
使用try and catch
try{} catch(e) { console.error(e); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.