我正在尝试在我的 Ionic 2 应用程序中向app.component.ts
文件添加导航。由于某种原因,我收到一个错误,指出未定义nav
。
添加NavController
也不是选项,因为它会说没有NavController
的提供程序。
我收到的错误是:
本地:尝试调用Facebook.browserInit,但Cordova不可用。确保包含 cordova.js 或在设备/模拟器主电源中运行.js (416,9)
Angular 正在开发模式下运行。调用 enableProdMode() 以启用生产模式。main.js (3511,5)Native
:尝试调用 NativeStorage.getItem,但 Cordova 不可用。确保包含 cordova.js 或在设备/模拟器主服务器中运行.js (416,9)
本机:尝试调用 StatusBar.styleDefault,但 Cordova 不可用。确保包含 cordova.js 或在设备/模拟器主服务器中运行.js (416,9)异常: 未捕获 (承诺):类型错误: 无法
获取未定义或空引用的属性"nav" 类型错误: 无法在匿名函数 (http://localhost:8100/build/main.js:
125439:17) at
t.prototype.invoke (http://localhost:8100/build/polyfills.js:3:9569) at
onInvoke (http://localhost:8100/build/main.js:38701:21)at t.prototype.invoke (http://localhost:8100/build/polyfills.js:3:9569) at e.prototype.run (http://localhost:8100/build/polyfills.js:3:6993) at Anonymous function (http://localhost:8100/build/polyfills.js:3:4652)at t.prototype.invokeTask (http://localhost:8100/build/
polyfills.js:3:10175)
at onInvokeTask (http://localhost:8100/build/main.js:38692:21)
at t.prototype.invokeTask (http://localhost:8100/build/polyfills.js:3:10175)at e.prototype.runTask (http://localhost:8100/build/polyfills.js:3:7611)at i (http://localhost:8100/build/polyfills.js:3:
3700)
at invoke (http://localhost:8100/build/polyfills.js:3:11431) main.js (78327,9)
原始堆栈跟踪:main.js (78332,13)错误:未
捕获(承诺):
类型错误:无法获取未定义或空引用
的属性"nav"TypeError:无法在匿名函数 (http://localhost:8100/build/main.js:125439:17)at t.prototype.invoke (http://localhost:8100/build/polyfills.js:3:9569)at onInvoke (http://localhost:8100/build/main.js:38701:21)at t.prototype.invoke (http://localhost:8100/build/polyfills.js:3:
9569) at
e.prototype.run (http://localhost:8100/build/polyfills.js:3:6993)
at Anonymous function (http://localhost:8100/build/polyfills.js:3:4652)at t.prototype.invokeTask (http://localhost:8100/build/polyfills.js:3:10175)at onInvokeTask (http://localhost:8100/build/main.js:38692:21)at t.prototype.invokeTask (http://localhost:8100/build/polyfills.js:3:10175)at e.prototype.runTask (http://localhost:8100/build/polyfills.js:3:
7611)
at i (http://localhost:8100/build/polyfills.js:3:3700)
at invoke (http://localhost:8100/build/polyfills.js:3:11431)at v (http://localhost:8100/build/polyfills.js:3:4856)at s (http://localhost:8100/build/polyfills.js:3:4283)at Anonymous function (http://localhost:8100/build/polyfills.js:3:4690)at t.prototype.invokeTask (http://localhost:8100/build/
polyfills.js:3:10175)
at onInvokeTask (http://localhost:8100/build/main.js:38692:21)
at t.prototype.invokeTask (http://localhost:8100/build/polyfills.js:3:10175)at e.prototype.runTask (http://localhost:8100/build/polyfills.js:3:7611)at i (http://localhost:8100/build/polyfills.js:3:3700)at invoke (http://localhost:8100/build/polyfills.js:3:11431) main.js (78333,13)
我正在使用的代码:
import { Component, ViewChild } from '@angular/core';
import { Nav, Platform } from 'ionic-angular';
import { StatusBar, Splashscreen, NativeStorage, Facebook } from 'ionic-native';
import { HomePage } from '../pages/home/home';
import { LoginPage } from '../pages/login/login';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
@ViewChild(Nav) nav: Nav;
rootPage = LoginPage;
constructor(private platform: Platform) {
this.InitliazeApp();
}
InitliazeApp() {
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.
NativeStorage.getItem('user')
.then(function (data) {
this.nav.setRoot(HomePage);
Splashscreen.hide();
}, function (error) {
this.nav.setRoot(LoginPage);
Splashscreen.hide();
});
StatusBar.styleDefault();
});
}
}
在 Ionic2 中,您通常通过将 NavController 添加到构造函数来添加导航。 然后使用依赖注入注入它。
constructor(public navCtrl: NavController)
在 app.component.ts 中导入以下内容
从"ionic-angular"导入 { Nav, NavController, Platform }
;
在构造函数中添加以下代码
构造函数(公共导航控制:导航控制器)