2017-05-02 30 views
0

我得到的错误firebase.auth()onAuthStateChanged无提供AngularFire

未捕获的(在承诺):错误:没有提供程序AngularFire!

当我试图拨打firebase.auth().onAuthStateChanged。 不知道为什么会发生这种情况。请帮忙。

import { Component } 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 { Login } from '../pages/login/login'; 

import firebase from 'firebase'; 

@Component({ 
    templateUrl: 'app.html' 
}) 
export class MyApp { 
    rootPage:any = Login; 
    isAuthenticated = false; 

    constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) { 
    firebase.initializeApp({ 
     apiKey: "AIzaSyC94rD8wXG0aRLTcG29qVGw8CFfvCK7XVQ", 
     authDomain: "myfirstfirebaseproject-6da6c.firebaseapp.com", 
    }); 

    firebase.auth().onAuthStateChanged(user => { 
     if (user) { 
     this.isAuthenticated = true; 
     this.rootPage = HomePage; 
     } else { 
     this.isAuthenticated = false; 
     this.rootPage = Login; 
     } 
    }); 

    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. 
     statusBar.styleDefault(); 
     splashScreen.hide(); 
    }); 
    } 
} 
+1

你可以添加app.module.ts吗? –

+0

我需要在应用程序模块中添加什么? – Jason

+0

您的项目是否包含在app.module.ts中注入的angularfire?您可以将上述文件的内容粘贴为@suraj –

回答

0

您的配置设置不正确。你应该在你的app.module.ts中有这个代码,而不是在你的组件中。

import { 
    AngularFireModule, 
    AuthMethods, 
    AuthProviders 
} from 'angularfire2'; 

... 

@NgModule({ 
    bootstrap: [AppComponent], 
    declarations: [AppComponent], 
    imports: [ 
     AngularFireModule.initializeApp({ 
      apiKey: '<some-key>', 
      authDomain: '<some-project-authdomain>', 
      databaseURL: '<some-database-URL>', 
      storageBucket: '<some-storage-bucket>' 
     }, { 
      method: AuthMethods.Password, 
      provider: AuthProviders.Password 
     }), 
     BrowserModule, 
     ... 
    ] 
}) 
class AppModule {} 

platformBrowserDynamic().bootstrapModule(AppModule); 

这是你应该有上述代码的地方。如果您没有设置提供商,您将不断收到错误消息。