AngularFire没有提供程序-使用核心模块/应用程序模块
我在aAngularFire没有提供程序-使用核心模块/应用程序模块,angular,typescript,firebase,singleton,Angular,Typescript,Firebase,Singleton,我在aAngular 2应用程序中有一个核心模块,在这个模块中,我正在尝试创建我的firebase配置的一个单实例 我的核心模块: import { NgModule, ModuleWithProviders, Optional, SkipSelf } from '@angular/core'; import { FirebaseConfigService } from './service/firebase-config.service'; @NgModule({ imports: []
Angular 2
应用程序中有一个核心模块
,在这个模块中,我正在尝试创建我的firebase配置的一个单实例
我的核心模块:
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
函数I reference 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\u配置来初始化firebase连接,该配置包括:
export const FIREBASE_CONFIG = {
apiKey: "apiKey",
authDomain: "projectId.firebaseapp.com",
databaseURL: "https://databaseName.firebaseio.com",
storageBucket: "bucket.appspot.com"
})
然后将此核心模块
导入应用程序模块
我遇到的问题是当我尝试执行以下操作时:
constructor(af: AngularFire) {
}
在我的app.component.ts
中,我得到以下错误:AngularFire没有提供程序
有人能分享一些关于我如何让FirebaseConfigService
在CoreModule
中工作的知识吗 我不确定是否需要这些。为什么会有这么大的偏差?为什么不直接从'angularfire2'导入{AngularFire}代码>然后是“构造函数(af:AngularFire){}”?@Kato我使用的是一个核心模块,它是一个单例模块,这样我就可以在这个模块中初始化Firebase,并在整个应用程序中将其作为一个实例共享。目的是什么?如果目标是获取单个数据集合,那么在您的核心模块中,this.singletondata=af.database.list(…)
可以很好地获得一个可以在整个应用程序中共享的列表。如果您试图将AngularFire库本身变成一个单例库,那么您就是在浪费时间优化库,库本身应该能够处理任何重要的事情。