简体   繁体   English

在 Angular 中动态配置 Firebase 环境

[英]Configure Firebase environment dynamically in Angular

I have a list of data in my mobile app and for each data I can have different firebase configuration file.我的移动应用程序中有一个数据列表,对于每个数据,我可以拥有不同的 Firebase 配置文件。 Requirement is, When I click on any data, it will make a request to our server and pull the firebase configuration from there.要求是,当我点击任何数据时,它会向我们的服务器发出请求并从那里提取 firebase 配置。

Once the configuration is available to Mobile app, it will use this configuration to connect with firebase and then retrieve data and perform some action.一旦配置可用于移动应用程序,它将使用此配置与 firebase 连接,然后检索数据并执行一些操作。 要求

Is it possible to achieve this?有可能实现这一目标吗?

Or more specifically, Can I do AngularFireModule.initializeApp(environment) in my constructor or in ngOnInit()in mycomponent.component.ts rather than doing it in myModuleName.Module.ts .或者更具体地说,我可以在我的构造函数中或在 mycomponent.component.ts 中的 ngOnInit() 中执行 AngularFireModule.initializeApp(environment) 而不是在 myModuleName.Module.ts 中执行它。

By doing AngularFireModule.initializeApp(environment) , you do two things :通过执行AngularFireModule.initializeApp(environment) ,您可以做两件事:

  • initializing the AngularFire module with your environment variables使用您的环境变量初始化 AngularFire 模块
  • importing the module导入模块

So you need to keep at least the import part in a module.所以你至少需要在模块中保留导入部分。 Maybe you can check if the firebase module allows initialization after import, which would mean :也许您可以检查 firebase 模块是否允许在导入后进行初始化,这意味着:

  1. import the module (in a module)导入模块(在模块中)
  2. later (when you get the configuration) 'initialize' the Firebase module稍后(当您获得配置时)“初始化”Firebase 模块

If this is not possible, I would suggest to lazy load your firebase module : https://angular.io/guide/lazy-loading-ngmodules如果这是不可能的,我建议延迟加载您的 firebase 模块: https : //angular.io/guide/lazy-loading-ngmodules

Nb: since you need to initialize the firebase module, you will probably need to wrap the Firebase module inside a standard module.注意:由于您需要初始化 firebase 模块,您可能需要将 Firebase 模块包装在标准模块中。

This is how I get it done.这就是我完成它的方式。 Rather than using it in app module, I specifically used it for the module which needed it.我没有在 app 模块中使用它,而是专门将它用于需要它的模块。

In module.ts在module.ts

    import { AngularFirestoreModule } from 'angularfire2/firestore';
    import { AngularFireDatabaseModule } from 'angularfire2/database';
    @NgModule({
      declarations: [
       ...
      ],
      imports: [
        ...
        AngularFirestoreModule,
        AngularFireDatabaseModule],
      exports: [
       ...      ],
      providers: [
       ...
      ]
    })

In the component.ts在 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= "";
  }

And a service to set firebase environment info.以及设置 firebase 环境信息的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM