未捕获(承诺):错误:没有AngularFireAuth的提供者

时间:2017-05-12 02:54:56

标签: firebase ionic2 firebase-authentication angularfire2 google-login

我们尝试使用google身份验证登录(Firebase / ionic2 / angularjs2)。我们的代码

 import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Observable } from 'rxjs/Observable';
import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
   user: Observable<firebase.User>;
  constructor(public navCtrl: NavController,public afAuth: AngularFireAuth) {
    this.user = afAuth.authState;
  }
  login() {
    this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider());
  }

  logout() {
    this.afAuth.auth.signOut();
  }
}

但我们收到错误:

Error: Uncaught (in promise): Error: No provider for AngularFireAuth!
Error: No provider for AngularFireAuth!

请指导我们在代码中使用的内容。

4 个答案:

答案 0 :(得分:24)

澄清@rmalviya建议的内容,我假设您目前使用的是Ionic版本3.x.x,对于此版本,您有两种方法可以导入本机插件并为插件添加相应的提供程序。

1)您可以在当前页面打字稿文件中添加提供程序。像这样:

  import { AngularFireAuth } from 'angularfire2/auth';

  ...

  @Component({
    selector: 'page-home',
    templateUrl: 'home.html',
    providers: [AngularFireAuth]
  })

2)您可以在app.modules.ts中导入第二种方法并将插件添加到提供者中

 import { AngularFireAuth } from 'angularfire2/auth';

 ...

 providers: [
   StatusBar,
   SplashScreen,
   {provide: ErrorHandler, useClass: IonicErrorHandler},
   AngularFireAuth
 ]

答案 1 :(得分:2)

resolve here https://github.com/iglewski/Annotator/issues/3

app.component.spec.ts:

import { FirebaseApp, FirebaseAppConfig, AngularFireModule } from 'angularfire2';
import { AngularFireAuth, AngularFireAuthModule } from 'angularfire2/auth';
import { AngularFireDatabaseModule } from 'angularfire2/database';
import * as firebase from 'firebase/app';
import { firebaseConfig } from './app.module';
describe('AppComponent', () => { 
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
      imports: [
        AngularFireModule.initializeApp(firebaseConfig), //ajout
        AngularFireAuthModule, //ajout
        AngularFireDatabaseModule //ajout
      ],
    }).compileComponents();

  })); 

答案 2 :(得分:1)

如果您正在使用IonicPageModule系统,那么您需要在提供商的AngularFireAuthapp.module.ts中导入page.module.ts阵列。

app.module.ts:

@NgModule({
... 
 providers: [AngularFireAuth]
... 

page.module.ts:

@NgModule({
  declarations: [
    SignupPage,
  ],
  imports: [
    IonicPageModule.forChild(SignupPage)
  ],
  exports: [
    SignupPage
  ],
  providers: [
    AngularFireAuth
  ]
})

答案 3 :(得分:1)

我也遇到了同样的问题,但是我设法通过在核心模块中添加以下几行来解决了这个问题。

CoreModule包含的代码将用于实例化您的应用程序并加载一些核心功能。

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

/* 3e. Import the angularfire2 thingy. */
**import {AngularFirestoreModule } from 'angularfire2/firestore';
import { AngularFireStorageModule } from 'angularfire2/storage';
import { AngularFireAuthModule } from 'angularfire2/auth';**

import { AuthModule } from '../auth/auth.module';
import { AuthService } from '../core/auth.service';

@NgModule({
  declarations: [],
  imports: [
    CommonModule,

    /* To allow the db to talk to the form. */
    **AuthModule,
    AngularFireAuthModule,
    AngularFireStorageModule,
    AngularFirestoreModule,**
  ],
  exports: [],
  providers: [
    AuthService,
  ]
})
export class CoreModule { }