Ionic2 Angular2 Typescript2:NgModule Imports何时在Cordova上运行?

时间:2016-11-13 16:58:38

标签: angular firebase ionic2 angularfire2 typescript2.0

当我尝试使用Google+登录我的Ionic2 / Angular2应用程序时,当我“离子运行android”(模拟器或在区域设备上)时,我一直收到“空引用”错误。但是当我使用“离子发球”时,我没有得到那个错误。

“null引用”似乎表示在我作为Android运行时尝试使用Firebase之前未运行AngularFireModule.initializeApp(firebaseConfig)

问题:

  1. @NgModule声明的“导入”部分何时运行 Cordova vs作为网络浏览器观看时?
  2. 我的猜测是正确的 AngularFireModule.initializeApp(firebaseConfig)未运行 在科尔多瓦?如果是这样,我该如何最好地确认?
  3. app.module.ts

    import { NgModule } from '@angular/core';
    import { IonicApp, IonicModule } from 'ionic-angular';
    import { MyApp } from './app.component';
    
    import { FormsModule } from '@angular/forms';
    
    // Pages
    import { ForgotPasswordPage } from '../pages/auth/forgot-password/forgot-password';
    import { AuthPage } from '../pages/auth/home/home';
    import { LoginEmailPage } from '../pages/auth/login-email/login-email';
    import { AboutPage } from '../pages/about/about';
    import { ContactPage } from '../pages/contact/contact';
    import { SignUpPage } from '../pages/auth/sign-up/sign-up';
    import { HomePage } from '../pages/home/home';
    import { TermsOfServicePage } from '../pages/terms-of-service/terms-of-service';
    import { TabsPage } from '../pages/tabs/tabs';
    
    import { AngularFireModule } from 'angularfire2';
    
    // Providers
    import { DataProvider } from '../providers/data';
    import { AuthProvider } from '../providers/auth';
    
    export const firebaseConfig = {
      apiKey: '****',
      authDomain: '****.firebaseapp.com',
      databaseURL: 'https://****.firebaseio.com/',
      storageBucket: '****.appspot.com',
    };
    
    @NgModule({
      declarations: [
        MyApp,
        ForgotPasswordPage,
        AuthPage,
        LoginEmailPage,
        SignUpPage,
        AboutPage,
        ContactPage,
        TermsOfServicePage,
        HomePage,
        TabsPage
      ],
      imports: [
        AngularFireModule.initializeApp(firebaseConfig),
        IonicModule.forRoot(MyApp),
        FormsModule
      ],
      bootstrap: [IonicApp],
      entryComponents: [
        MyApp,
        ForgotPasswordPage,
        AuthPage,
        LoginEmailPage,
        SignUpPage,
        AboutPage,
        ContactPage,
        HomePage,
        TabsPage,
        TermsOfServicePage
      ],
      providers: [
        DataProvider,
        AuthProvider
      ]
    })
    export class AppModule {}
    

    Google登录auth.ts的一部分

    loginWithGooglePlus() {
      return Observable.create(observer => {
        if (this.platform.is('cordova')) {
          GooglePlus.login(['public_profile', 'email']).then(googlePlusData => {
            let provider = firebase.auth.GoogleAuthProvider.credential(googlePlusData.authResponse.accessToken);
            firebase.auth().signInWithCredential(provider).then(firebaseData => {
              // process user data
              observer.next();
            });
          }, error => {
            observer.error(error);
          });
        } else {
          this.af.auth.login({
            provider: AuthProviders.Google,
            method: AuthMethods.Popup
          }).then((googlePlusData) => {
            // process user data
            observer.next();
          }).catch((error) => {
            console.info("error", error);
            observer.error(error);
          });
        }
      });
    }
    

    错误(如Android Studio中所示,使用“离子生成安卓”文件)

    E/PluginManager: Uncaught exception from plugin
                 java.lang.NullPointerException: null reference
                     at com.google.android.gms.common.internal.zzaa.zzy(Unknown Source)
                     at com.google.android.gms.auth.api.signin.internal.zzc.getSignInIntent(Unknown Source)
                     at nl.xservices.plugins.GooglePlus.signIn(GooglePlus.java:165)
                     at nl.xservices.plugins.GooglePlus.execute(GooglePlus.java:70)
                     at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:117)
                     at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:98)
                     at org.apache.cordova.PluginManager.exec(PluginManager.java:132)
                     at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:57)
                     at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41)
                     at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
                     at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:53)
                     at android.os.Handler.dispatchMessage(Handler.java:102)
                     at android.os.Looper.loop(Looper.java:148)
                     at android.os.HandlerThread.run(HandlerThread.java:61)
    

    这是在config.xml文件中

    <plugin name="cordova-plugin-googleplus" spec="~5.1.1">
        <variable name="REVERSED_CLIENT_ID" value="com.****.****" />
    </plugin>
    

1 个答案:

答案 0 :(得分:0)

你在这里使用的是什么:

<plugin name="cordova-plugin-googleplus" spec="~5.1.1">
    <variable name="REVERSED_CLIENT_ID" value="com.****.****" />
</plugin>

我遇到了完全相同的问题,并意识到当您在Google Developers Console中实际传递反向网络ID时,我正在传递我的包ID com.javebratt.packagename,所以它类似于:{{1} }

然后它开始工作了一半时间并打破另一半,所以我决定将webId传递给com.googleusercontent.apps.<uniqueIdHere>函数,它开始工作没有任何问题。