无法将NavController添加到我的app.component.ts

时间:2018-06-28 21:32:42

标签: ionic3

我想将NavController注入到我的app.component.ts中,但是在运行我的应用程序时,我不断收到错误消息:

NullInjectorError: No provider for NavController!

就导入和声明它而言,我正在做所有正确的事情,就像我注入的任何其他组件一样。但是在我的主要应用程序组件上,它似乎并不喜欢它。

那里发生了什么事?

代码:

import { AuthenticationProvider } from './../providers/authentication/authentication';
import {Component} from '@angular/core';
import { Events, Platform, NavController } from 'ionic-angular';
import {StatusBar} from '@ionic-native/status-bar';
import {SplashScreen} from '@ionic-native/splash-screen';
import {ScreenOrientation} from '@ionic-native/screen-orientation';




import {TabsPage} from '../pages/tabs/tabs';
import {LoginPage} from "../pages/login/login";

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage: any = TabsPage;

  constructor(
    platform: Platform,
    statusBar: StatusBar,
    splashScreen: SplashScreen,
    events: Events,
    private navCtrl: NavController,
    private authProvider: AuthenticationProvider,
    screenOrientation: ScreenOrientation) {
    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.styleLightContent();
      splashScreen.hide();

      //device-specific code, such as detecting screen rotation
      if (platform.is('cordova')) {
        screenOrientation.lock(screenOrientation.ORIENTATIONS.PORTRAIT);
      }

      events.subscribe('user:expired_token', () => {
        this.rootPage = LoginPage;
      });
    });
  }

  logout(){
    this.authProvider.logout();
    this.navCtrl.setRoot('LoginPage');
  }
}

1 个答案:

答案 0 :(得分:3)

您可以通过以下方式访问app.component.ts中的<ion-nav #nav> ... @ViewChild('nav') public nav: NavController;

NavController

原因是repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } allprojects { repositories { google() jcenter() } 在引导根组件时尚未初始化,因此无法注入。查看source code

中的评论