2017-08-31 15 views
1

私はちょうどそのエラーを理解していません、なぜそれはプロパティ 'nav'を読むことができません。私は混乱しています。誰も私を助け、私にエラーを説明することを願って、ここでのエラーは次のとおりです。問題のようですイオン2:ランタイムエラー未知(未定):TypeError:未定義のプロパティ 'nav'を読み取ることができません

import { Component, ViewChild } from '@angular/core'; 
import { Platform, Nav } from 'ionic-angular'; 
import { StatusBar } from '@ionic-native/status-bar'; 
import { SplashScreen } from '@ionic-native/splash-screen'; 
import { NativeStorage } from '@ionic-native/native-storage'; 
import { GooglePlus } from '@ionic-native/google-plus'; 

import { AccountPage } from '../pages/account/account'; 
import { LoginPage } from '../pages/login/login'; 

import { TabsPage } from '../pages/tabs/tabs'; 

@Component({ 
    templateUrl: 'app.html' 
}) 
export class MyApp { 
    @ViewChild('nav') nav:Nav; 

    rootPage:any = TabsPage; 

    constructor(public nativeStorage: NativeStorage, public googlePlus: GooglePlus, platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) { 

     platform.ready().then(() => { 

     let env = this; 

     this.googlePlus.trySilentLogin({ 
     'scopes': '', // optional, space-separated list of scopes, If not included or empty, defaults to `profile` and `email`. 
     'webClientId': 'webClientId.apps.googleusercontent.com', // optional clientId of your Web application from Credentials settings of your project - On Android, this MUST be included to get an idToken. On iOS, it is not required. 
     'offline': true 
     }) 
     .then(function(data) { 
     this.nav.push(AccountPage); 
     this.splashScreen.hide(); 
     }, function (error){ 
     this.nav.push(LoginPage); 
     this.splashScreen.hide(); 
     }); 

     statusBar.styleDefault(); 
    }); 
    } 
} 

何?

答えて

1

標準機能を使用しているため、thisキーワードが上書きされ、コンポーネントではなくその機能が参照されるという問題があります。その問題を回避するには、このように、矢印機能を使用する必要があります。

// ... 

@Component({ 
    templateUrl: 'app.html' 
}) 
export class MyApp { 
    @ViewChild('nav') nav:Nav; 

    rootPage:any = TabsPage; 

    constructor(public nativeStorage: NativeStorage, 
       public googlePlus: GooglePlus, 
       public platform: Platform, 
       public statusBar: StatusBar, 
       public splashScreen: SplashScreen) { 

    platform.ready().then(() => { 

     let env = this; 

     this.googlePlus.trySilentLogin({ 
      'scopes': '', // optional, space-separated list of scopes, If not included or empty, defaults to `profile` and `email`. 
      'webClientId': 'webClientId.apps.googleusercontent.com', // optional clientId of your Web application from Credentials settings of your project - On Android, this MUST be included to get an idToken. On iOS, it is not required. 
      'offline': true 
     }) 
     .then(
      data => { // <---------------- Here! 
       this.nav.push(AccountPage); 
       this.splashScreen.hide(); 
      }, 
      error => { // <---------------- Here! 
       this.nav.push(LoginPage); 
       this.splashScreen.hide(); 
      }); 

     statusBar.styleDefault(); 
    }); 
    } 
} 
+1

はあなたに非常に多くの空気を感謝し、それは私のために動作します。 – Patrick

+0

それを聞いてうれしい! :) – sebaferreras

関連する問題