2017-03-23 170 views
0

我正尝试将导航添加到我的Ionic 2应用程序中的app.component.ts文件。出于某种原因,我收到一个错误,nav未定义。Ionic 2 - Nav undefined

添加NavController也是没有选择的,因为它会说没有供应商NavController

我收到错误(S)是:

Native: tried calling Facebook.browserInit, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator main.js (416,9)
Angular is running in the development mode. Call enableProdMode() to enable the production mode. main.js (3511,5)
Native: tried calling NativeStorage.getItem, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator main.js (416,9)
Native: tried calling StatusBar.styleDefault, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator main.js (416,9)
EXCEPTION: Uncaught (in promise):
TypeError: Unable to get property 'nav' of undefined or null reference TypeError: Unable to get property 'nav' of undefined or null reference
at Anonymous function (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)
ORIGINAL STACKTRACE: main.js (78332,13)
Error: Uncaught (in promise):
TypeError: Unable to get property 'nav' of undefined or null reference
TypeError: Unable to get property 'nav' of undefined or null reference at Anonymous function (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(); 
    }); 
    } 
} 
+0

添加提供程序在ngModules –

+0

@varunaaruru我使用Angular2在我的项目,我应该添加这个的任何想法?通过在我的'app.module.ts'中添加NavController,我收到了很多错误,所以这可能不是它。 – Peurr

+0

你可以显示html端吗? –

回答

0

在Ionic2你通常加入NavController给构造函数添加导航。然后使用依赖注入进行注入。

constructor(public navCtrl: NavController) 
0

导入在app.component.ts以下

import { Nav, NavController, Platform } from 'ionic-angular';

添加以下代码构造

constructor(public navCtrl: NavController)

+0

请添加您的解决方案,而不是只是代码的要点。 – pirho

+0

这些是需要在给定的代码上面编辑的主线@pirho –

+0

您可以分享您的代码..您究竟在寻找什么 –