Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2: EXCEPTION: No provider for NavController

i have a problem with my ionic 2/angular 2 app.

I got an app.ts where the hole "auth" part is implementet.

The code looks like this:

 import {Nav, Platform, Modal, ionicBootstrap} from "ionic-angular";
import {NavController} from "ionic-angular/index";
import {StatusBar} from "ionic-native";
import {Component, ViewChild} from "@angular/core";
import {AngularFire, FirebaseListObservable, FIREBASE_PROVIDERS, defaultFirebase} from "angularfire2";
import {HomePage} from "./pages/home/home";
import {AuthPage} from "./pages/auth/home/home";

@Component({
  templateUrl: "build/app.html",
})

class MyApp {
  @ViewChild(Nav) nav: Nav;

  authInfo: any;
  rootPage: any = HomePage;
  pages: Array<{title: string, component: any}>;

  constructor(private platform: Platform, private navCtrl: NavController, private af: AngularFire) {
    this.initializeApp();

    this.pages = [
      { title: "Home", component: HomePage }
    ];

  }

  initializeApp() {
    this.platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      StatusBar.styleDefault();
    });
  }

  openPage(page) {
    this.nav.setRoot(page.component);
  }

  ngOnInit() {
    this.af.auth.subscribe(data => {
      if (data) {
        this.authInfo = data;
      } else {
        this.authInfo = null;
        this.showLoginModal();
      }
    });
  }

  logout() {
    if (this.authInfo) {
      this.af.auth.logout();
      return;
    }
  }

  showLoginModal() {
    let loginPage = Modal.create(AuthPage);
    this.navCtrl.present(loginPage);
  }
}

But now, when i try to run the app i get this message:

ORIGINAL EXCEPTION: No provider for NavController

Do you have any idea how to solve this problem? Thanks!

like image 731
Fargho Avatar asked Jun 30 '16 11:06

Fargho


People also ask

How do you pass data in Navcontroller ionic?

To push a new view onto the navigation stack, use the push method. If the page has an <ion-navbar> , a back button will automatically be added to the pushed view. Data can also be passed to a view by passing an object to the push method. The pushed view can then receive the data by accessing it via the NavParams class.

How do you send data from one page to another in ionic?

To Pass data from Home page to About page we will need to import NavParams class. Since, I am using Ionic CLI to generate pages, class NavParams will already be imported in the about page.

How do you pass data from one component to another in ionic 4?

In Ionic 4 you can pass data with the modalController using ComponentProps and @Input() making it more suitable of a quick push/pop implementation.


1 Answers

You can not inject a NavController in a Root component via a constructor.

So, basically you can not do something like below:-

constructor(private nav: NavController){
}

This is how you can inject a NavController

@Controller({
  ....
})
class MyApp{
  @ViewChild('nav') nav: NavController;
  ....
  ....
  constructor(...){ // See, no NavController here
  }
  ....
}

And this is what Ionic docs has to say.

What if you want to control navigation from your root app component? You can't inject NavController because any components that are navigation controllers are children of the root component so they aren't available to be injected.

By adding a reference variable to the ion-nav, you can use @ViewChild to get an instance of the Nav component, which is a navigation controller (it extends NavController)

like image 138
Mav55 Avatar answered Oct 11 '22 09:10

Mav55