我的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
有人可以分享一些有关如何让FirebaseConfigService
在CoreModule
内工作的知识吗?