Can't seem to add a Service as provider to app.component.ts

时间:2018-02-26 17:47:41

标签: angular typescript firebase ionic2

I need to add the provider to share a variable with app.component.ts, because I'm looking for the logged user data in real time in my app, but I can't seem to add the provider to the constructor, I keep getting the following error.

Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp()

Here's a snapshot of my code:

app.component.ts :

import { UsersServiceProvider } from '../providers/users-service/users-service';
@Component({
  templateUrl: 'app.html',
})

export class MyApp {
    @ViewChild(Nav) navCtrl: Nav;
    rootPage:any = InicioPage;

    constructor(public userServices : UsersServiceProvider) //another imports too
    platform.ready().then(() => {
      statusBar.styleDefault();
      splashScreen.hide();
    });
    var config = {
         //some config for app and firebase
    };
    firebase.initializeApp(config);
    var that = this;
    firebase.auth().onAuthStateChanged((user) => {
        if (user) {
        // Need to set variable on userServices HERE
        }
    })
};

user-service.ts

import { Injectable } from '@angular/core';
import {AlertController , Platform} from 'ionic-angular'
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import * as firebase from 'firebase';

@Injectable()
export class UsersServiceProvider {

public currentUser : any;
 constructor(private platform: Platform, public alertCtrl: AlertController, public http: Http) {
}

setCurrentUser(user){
  this.currentUser = user; // WANT TO SET IT HERE
}

}

Everything is also OK at the app.module.ts

1 个答案:

答案 0 :(得分:0)

我对Firebase不熟悉,但您是否尝试在app.module.ts中添加该服务?在将它注入任何课程之前,你必须这样做。它应该是这样的:

@NgModule({
declarations: [],
imports: [],
providers: [UsersServiceProvider]
})