我正在绑定将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');
}
}发布于 2018-06-28 21:45:07
您可以通过以下方式到达NavController in app.component.ts:
<ion-nav #nav>
...
@ViewChild('nav')
public nav: NavController;原因是当根组件正在引导时还没有初始化NavController,所以不能注入它。参见源代码中的评论
https://stackoverflow.com/questions/51091519
复制相似问题