How to import NavController in Ionic 2?


#1

When I trying to import NavController I’m receiving strange error:

Error in ./MyApp class MyApp_Host - caused by: No provider for
NavController!

My code:

import { Component, ViewChild } from '@angular/core';
import { Events, Platform, Nav, NavController } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';

import { Storage } from '@ionic/storage';

// Pages
import { HomePage } from '../pages/home/home';
import { LoginPage } from '../pages/login/login';

// Providers
import { User } from '../providers/user';

@Component({
	templateUrl: 'app.html'
})

export class MyApp {
	rootPage: any = HomePage;

	@ViewChild(Nav) nav: Nav;

	constructor(
		private platform: Platform, 
		private events: Events,
		private storage: Storage,
		private User: User,
		private navCtrl: NavController
	) {
		platform.ready().then(() => {
			StatusBar.styleDefault()

			User.isLogged().then(is_logged => {
				if (is_logged == false) {
					this.rootPage = LoginPage
				}

				Splashscreen.hide()
			})
		})
	}

	listenToLoginEvents() {
		this.events.subscribe('user:login', () => {
			// this.navCtrl.push(HomePage)
			// this.navCtrl.setRoot(HomePage);
		})

		this.events.subscribe('user:logout', () => {
			console.log('user:logout')
		})
	}
}

#2

what version of ionic 2?


#3

If you really must do this, use the nav property you’ve already got. But you should be able to achieve what you’re looking for by just modifying rootPage and not futzing with the NavController at all.