没有AngularFire的提供者 - 使用核心模块/应用程序模块

时间:2016-12-28 08:56:32

标签: angular typescript firebase singleton

我的core module应用程序中有Angular 2,在此模块中我正在尝试创建firebase configuration的单例实例。

我的核心模块:

import { NgModule, ModuleWithProviders, Optional, SkipSelf } from '@angular/core';
import { FirebaseConfigService } from './service/firebase-config.service';


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

export class CoreModule {

// Checks to see if an instance of core module already exists, if so throw and error
constructor( @Optional() @SkipSelf() parentMoedule: CoreModule) {

    if (parentMoedule){
        throw new Error("Core Module already exists. Only import in the root / app module");
    }

}

// this will only be called from app.module, if core module hasn't already 
// been created somewhere else within the application.
static forRoot(): ModuleWithProviders {
    return {
        ngModule: CoreModule,
        providers: [ FirebaseConfigService ]
    };
}

}

正如您在forRoot函数中看到的,我在提供程序中引用了FirebaseConfigService,FirebaseConfigService如下所示:

import { Injectable } from '@angular/core';

// https://github.com/angular/angularfire2
import { AngularFireModule } from 'angularfire2';

// Constants
import { FIREBASE_CONFIG } from '../constant/constants';



@Injectable()
export class FirebaseConfigService {
   constructor() {
       this.configureApp();
   }

   configureApp() {
      AngularFireModule.initializeApp(FIREBASE_CONFIG);

   }
}

configureApp内我通过引用FIREBASE_CONFIG来初始化我的firebase连接,其中包括:

export const FIREBASE_CONFIG = {
apiKey: "apiKey",
authDomain: "projectId.firebaseapp.com",
databaseURL: "https://databaseName.firebaseio.com",
storageBucket: "bucket.appspot.com"

};

然后我将Core Module导入App.Module我遇到的问题:

 constructor(af: AngularFire) {

 }

在我的app.component.ts中,我收到以下错误:AngularFire

没有提供商

有人可以分享一些有关如何让FirebaseConfigServiceCoreModule内工作的知识吗?

0 个答案:

没有答案