Angular 以动态方式配置Firebase环境
我的移动应用程序中有一个数据列表,对于每个数据,我可以有不同的firebase配置文件。 要求是,当我点击任何数据时,它将向我们的服务器发出请求,并从那里提取firebase配置 一旦配置可用于移动应用程序,它将使用此配置连接firebase,然后检索数据并执行某些操作。 有可能做到这一点吗Angular 以动态方式配置Firebase环境,angular,firebase,ionic-framework,firebase-realtime-database,angular-module,Angular,Firebase,Ionic Framework,Firebase Realtime Database,Angular Module,我的移动应用程序中有一个数据列表,对于每个数据,我可以有不同的firebase配置文件。 要求是,当我点击任何数据时,它将向我们的服务器发出请求,并从那里提取firebase配置 一旦配置可用于移动应用程序,它将使用此配置连接firebase,然后检索数据并执行某些操作。 有可能做到这一点吗 或者更具体地说,我可以在构造函数中或mycomponent.component.ts中的ngOnInit()中执行AngularFireModule.initializeApp(环境),而不是在myMod
或者更具体地说,我可以在构造函数中或mycomponent.component.ts中的ngOnInit()中执行AngularFireModule.initializeApp(环境),而不是在myModuleName.Module.ts中执行它。通过执行
AngularFireModule.initializeApp(环境)
,您可以执行两件事:
- 使用环境变量初始化AngularFire模块
- 导入模块
注意:由于您需要初始化firebase模块,您可能需要将firebase模块封装在标准模块中。我就是这样完成的。我没有在app模块中使用它,而是专门为需要它的模块使用它 在module.ts中
import { AngularFirestoreModule } from 'angularfire2/firestore';
import { AngularFireDatabaseModule } from 'angularfire2/database';
@NgModule({
declarations: [
...
],
imports: [
...
AngularFirestoreModule,
AngularFireDatabaseModule],
exports: [
... ],
providers: [
...
]
})
在component.ts中
import { AngularFirestore } from 'angularfire2/firestore';
import { _firebaseAppFactory } from "angularfire2/firebase.app.module";
angFirestore: AngularFirestore
ngOnInit(){
this.angFirestore = new AngularFirestore(_firebaseAppFactory(environment.firebase, environment.localAppName.toString()), false);
}
ionViewWillUnload(){
this.clearFirebaseInfo();
if(this.angFirestore && this.angFirestore.app){
this.angFirestore.app.delete();
}
}
clearFirebaseInfo(){
environment.apiKey= "";
environment.authDomain= "";
environment.databaseURL= "";
environment.projectId= " ";
environment.storageBucket= "";
environment.messagingSenderId= "";
environment.appId= "";
}
以及设置firebase环境信息的服务 您好,我正在寻找相同的功能。你能让它工作吗?谢谢,弗兰克