简体   繁体   English

this.nav 未定义 - Ionic2

[英]this.nav is undefined - Ionic2

I'm having a problem with this piece of code here:我在这里的这段代码有问题:

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);
    }
}

So, on the openPage() method, the this.nav (the nav is declared above) is returning an error: this.nav is undefined.因此,在 openPage() 方法中,this.nav(nav 在上面声明)返回错误:this.nav 未定义。 Someone has any idea why?有人知道为什么吗?

Try this.nav.rootNav.setRoot(page) .试试this.nav.rootNav.setRoot(page) This should work.这应该有效。 It works for me.这个对我有用。

One more thing, isn't user = this.navPrm.get('auth').current_user();还有一件事,不是user = this.navPrm.get('auth').current_user(); is supposed to be inside the constructor when you are retrieving the value from navParam .当您从navParam检索值时,应该在构造函数内。 (Since, I am not familiar with your code, not sure about this part though. Just seemed wrong.) (因为,我不熟悉你的代码,但不确定这部分。似乎错了。)

使用try and catch

try{} catch(e) { console.error(e); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM