首页 文章

离子2 - 导航未定义

提问于
浏览
0

我正在尝试将导航添加到Ionic 2应用程序中的 app.component.ts 文件中 . 由于某种原因,我收到的错误是 nav 未定义 .

添加 NavController 也没有选项,因为'll say that there'没有 NavController 的提供者 .

我收到的错误是:

Native:尝试调用Facebook.browserInit,但Cordova不可用 . 确保包含cordova.js或在设备/模拟器中运行main.js(416,9)Angular正在开发模式下运行 . 调用enableProdMode()以启用 生产环境 模式 . main.js(3511,5)Native:尝试调用NativeStorage.getItem,但Cordova不可用 . 确保包含cordova.js或在设备/模拟器中运行main.js(416,9)Native:尝试调用StatusBar.styleDefault,但Cordova不可用 . 确保包含cordova.js或在设备/模拟器中运行main.js(416,9)EXCEPTION:Uncaught(在promise中):TypeError:无法获取未定义或null引用的属性'nav'TypeError:无法获取属性匿名函数(http:// localhost:8100 / build / main.js:125439:17)at t.prototype.invoke(http:// localhost:8100 / build / polyfills.js)中未定义或空引用的'nav' :3:9569)在t.prototype.invoke上的onInvoke(http:// localhost:8100 / build / main.js:38701:21)(http:// localhost:8100 / build / polyfills.js:3:9569) )在e.prototype.run(http:// localhost:8100 / build / polyfills.js:3:6993)的匿名函数(http:// localhost:8100 / build / polyfills.js:3:4652)at t .prototype.invokeTask(http:// localhost:8100 / build / polyfills.js:3:10175)位于t.prototype.invokeTask的onInvokeTask(http:// localhost:8100 / build / main.js:38692:21) (http:// localhost:8100 / build / polyfills.js:3:10175)在e.prototype.runTask(http:// localhost:8100 / build / polyfills.js:3:7611)at i(http:/ /本地主机:8100 /编译/ polyfills .js:3:3700)at invoke(http:// localhost:8100 / build / polyfills.js:3:11431)main.js(78327,9)ORIGINAL STACKTRACE:main.js(78332,13)错误:未捕获(承诺):TypeError:无法获取undefined或null引用的属性'nav'TypeError:无法在匿名函数中获取未定义或空引用的属性'nav'(http:// localhost:8100 / build / main.js :125439:17)at.vototype.invoke(http:// localhost:8100 / build / polyfills.js:3:9569)atInvoke(http:// localhost:8100 / build / main.js:38701:21 )在t.prototype.invoke(http:// localhost:8100 / build / polyfills.js:3:9569)的e.prototype.run(http:// localhost:8100 / build / polyfills.js:3:6993) )在onInvokeTask的t.prototype.invokeTask(http:// localhost:8100 / build / polyfills.js:3:10175)的匿名函数(http:// localhost:8100 / build / polyfills.js:3:4652) (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://本地主机:8100 /构建/ polyfills.js:3 :7611)at i(http:// localhost:8100 / build / polyfills.js:3:3700)at voke(http:// localhost:8100 / build / polyfills.js:3:11431)v(http:http:// //localhost:8100/build/polyfills.js:3:4856)在s(http:// localhost:8100 / build / polyfills.js:3:4283)的匿名函数(http:// localhost:8100 / build) /polyfills.js:3:4690)at.vototype.invokeTask(http:// localhost:8100 / build / polyfills.js:3:10175)atInvokeTask(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)

The code I'm using:

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();
    });
  }
}

2 回答

  • 0

    在Ionic2中,您通常通过将NavController添加到构造函数来添加导航 . 然后使用依赖注入注入它 .

    constructor(public navCtrl: NavController)
    
  • 0

    在app.component.ts中导入以下内容

    import {nav,NavController,Platform}来自'ionic-angular';

    在构造函数中添加以下代码

    构造函数(public navCtrl:NavController)

相关问题