0

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. Someone has any idea why?

  • http://stackoverflow.com/questions/34947154/angular-2-viewchild-annotation-returns-undefined perhaps this link helps to figure out the nullpointer exception – TypedSource Apr 19 '17 at 21:10

1 Answers1

1

Try this.nav.rootNav.setRoot(page). This should work. It works for me.

One more thing, isn't user = this.navPrm.get('auth').current_user(); is supposed to be inside the constructor when you are retrieving the value from navParam. (Since, I am not familiar with your code, not sure about this part though. Just seemed wrong.)

Sagar Kulkarni
  • 2,072
  • 2
  • 16
  • 25