我在应用程序中使用Firebase。 Firebase配置存储在环境文件中。
export const environment = {
production: false,
firebase: {
apiKey: "api-key",
authDomain: "project-id.firebaseapp.com",
databaseURL: "https://project-id.firebaseio.com",
projectId: "project-id",
storageBucket: "project-id.appspot.com",
messagingSenderId: "sender-id",
appID: "app-id",
}
};
然后我使用该配置初始化了Firebase应用。
import { environment } from '../../../environments/environment';
import { AngularFireModule } from 'angularfire2';
@NgModule({
imports: [
AngularFireModule.initializeApp(environment.firebase),
AngularFireDatabaseModule,
],
declarations: [],
providers: [],
entryComponents: [],
})
export class InboxModule {}
到目前为止,太好了。但是我想根据品牌从API调用中获取firebase配置,然后根据响应初始化应用。
我写了一个Injectable Service,用于存储配置。
import { Injectable } from '@angular/core';
@Injectable()
export class FirebaseService {
private _firebaseConfig = null;
constructor() {}
get firebaseConfig() {
return this._firebaseConfig;
}
set firebaseConfig(val) {
this._firebaseConfig = val;
}
}
但是我无法在ngModule中使用它。
import { environment } from '../../../environments/environment';
import { AngularFireModule } from 'angularfire2';
import { FirebaseService } from '../services/firebase.service';
@NgModule({
imports: [
AngularFireModule.initializeApp(environment.firebase),
AngularFireDatabaseModule,
],
declarations: [],
providers: [],
entryComponents: [],
})
export class InboxModule {
constructor(private firebaseService: FirebaseService) {
AngularFireModule.initializeApp(this.firebaseService.firebaseConfig);
}
}
我遇到此错误。
Error: No provider for InjectionToken FirebaseAppConfigToken!
这是由于未正确初始化应用程序造成的。
export class InboxModule {
constructor(private firebaseService: FirebaseService) {
console.log(this.firebaseService.firebaseConfig) // Works
// Unable to initialize the App though.
AngularFireModule.initializeApp(this.firebaseService.firebaseConfig);
}
}
我想念什么吗?或解决此用例的任何其他方法?