“TypeError:无法读取属性'导航'的未定义”在离子

“TypeError:无法读取属性'导航'的未定义”在离子

问题描述:

我在两天前提出了类似的问题,但这次我得到同样的错误,当我在实际设备或模拟器中运行的代码。在普通的浏览器上,它工作正常。“TypeError:无法读取属性'导航'的未定义”在离子

这里是我的app.component.ts代码:

import { Component, ViewChild } from '@angular/core'; 
import { Platform } from 'ionic-angular'; 
import { StatusBar } from '@ionic-native/status-bar'; 
import { SplashScreen } from '@ionic-native/splash-screen'; 
import { HomePage } from '../pages/home/home'; 
import { LoginPage } from '../pages/login/login'; 
import {NativeStorage} from '@ionic-native/native-storage'; 
import { NavController } from 'ionic-angular'; 

@Component({ 
    templateUrl: 'app.html' 
}) 

export class MyApp { 
    rootPage:any = LoginPage; 
    @ViewChild('navApp') nav : NavController; 
    constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private nativeStorage: NativeStorage,) { 
    platform.ready().then(() => { 
     this.nativeStorage.getItem('userExists').then((data)=>{ 
     this.nav.push(HomePage); 
     console.log(data.userExists); 
     },(error)=>{ 
     console.log("No Data in Local Storage"); 
      this.nav.push(LoginPage); 
     }) 

     statusBar.styleDefault(); 
     splashScreen.hide(); 
    }); 
    } 

} 

这是我app.html

<ion-nav #navApp [root]="rootPage"></ion-nav> 

这是我的版本信息

global packages: 

    @ionic/cli-utils : 1.2.0 
    Cordova CLI  : 7.0.1 
    Ionic CLI  : 3.2.0 

local packages: 

    @ionic/app-scripts    : 1.3.7 
    @ionic/cli-plugin-cordova  : 1.2.1 
    @ionic/cli-plugin-ionic-angular : 1.2.0 
    Cordova Platforms    : android 6.2.3 
    Ionic Framework     : ionic-angular 3.2.1 

System: 

    Node  : v6.10.2 
    OS   : Windows 10 
    Xcode  : not installed 
    ios-deploy : not installed 
    ios-sim : not installed 

的package.json:

{ 
    "name": "myapp", 
    "version": "0.0.1", 
    "author": "Ionic Framework", 
    "homepage": "http://ionicframework.com/", 
    "private": true, 
    "scripts": { 
     "clean": "ionic-app-scripts clean", 
     "build": "ionic-app-scripts build", 
     "lint": "ionic-app-scripts lint", 
     "ionic:build": "ionic-app-scripts build", 
     "ionic:serve": "ionic-app-scripts serve" 
    }, 
    "dependencies": { 
     "@angular/common": "4.1.0", 
     "@angular/compiler": "4.1.0", 
     "@angular/compiler-cli": "4.1.0", 
     "@angular/core": "4.1.0", 
     "@angular/forms": "4.1.0", 
     "@angular/http": "4.1.0", 
     "@angular/platform-browser": "4.1.0", 
     "@angular/platform-browser-dynamic": "4.1.0", 
     "@ionic-native/core": "3.7.0", 
     "@ionic-native/facebook": "^3.10.2", 
     "@ionic-native/native-storage": "^3.10.3", 
     "@ionic-native/splash-screen": "3.7.0", 
     "@ionic-native/status-bar": "3.7.0", 
     "@ionic/storage": "2.0.1", 
     "cordova-android": "^6.2.3", 
     "cordova-plugin-console": "^1.0.5", 
     "cordova-plugin-device": "^1.1.4", 
     "cordova-plugin-facebook4": "^1.9.0", 
     "cordova-plugin-nativestorage": "^2.2.2", 
     "cordova-plugin-splashscreen": "^4.0.3", 
     "cordova-plugin-statusbar": "^2.2.2", 
     "cordova-plugin-whitelist": "^1.3.1", 
     "ionic-angular": "3.2.1", 
     "ionic-native": "^2.5.1", 
     "ionic-plugin-keyboard": "^2.2.1", 
     "ionicons": "3.0.0", 
     "rxjs": "5.1.1", 
     "sw-toolbox": "3.6.0", 
     "zone.js": "0.8.10" 
    }, 
    "devDependencies": { 
     "@ionic/app-scripts": "1.3.7", 
     "@ionic/cli-plugin-cordova": "^1.2.1", 
     "@ionic/cli-plugin-ionic-angular": "^1.2.0", 
     "typescript": "2.2.1" 
    }, 
    "description": "An Ionic project", 
    "cordova": { 
     "plugins": { 
      "cordova-plugin-console": {}, 
      "cordova-plugin-device": {}, 
      "cordova-plugin-splashscreen": {}, 
      "cordova-plugin-statusbar": {}, 
      "cordova-plugin-whitelist": {}, 
      "ionic-plugin-keyboard": {}, 
      "cordova-plugin-facebook4": { 
       "APP_ID": "305938313175123", 
       "APP_NAME": "myapp" 
      }, 
      "cordova-plugin-nativestorage": {} 
     }, 
     "platforms": [ 
      "android" 
     ] 
    } 
} 
+0

哪个平台以及您如何运行您的应用程序? –

+0

我正在使用Android模拟器和Galaxy Note 4与棉花糖操作系统。在模拟器上,我有Android N. –

+0

你可以添加package.json的问题? –

更新到最新的离子版本3.3.0解决了这个问题!

+0

很高兴听到:)请将此标记为接受的答案 – sebaferreras

为什么不只是改变rootPage的值而不是推动?

你有可变

rootPage:any = LoginPage; 

所以默认情况下,您的主页将是LoginPage。当您在本地存储中签入时,如果昵称存在,而不是推送页面,请使用

this.rootPage = NameofYourPageHere. 

这应该有效。