Firebase Angular 4 Initialize based on node environment

梦想的初衷 提交于 2020-01-03 09:07:37

问题


I've built my Angular 4 project using the Angular CLI. I am deploying my app on Heroku, I've created heroku pipelines for dev and production environment. I have two firebase database dev and production and I want my angular 2 apps to connect to the firebase database based on heroku config variables

I searched on google and found this answer helpful as @yoni-rabinovitch suggested to send an HTTP request on node server to check for the environment when the app initializes.

I am a beginner in Angular 4 and typescript and all I need to implement is to send an HTTP request and initialize the firebase module based on the response.

app.module.ts

import { environment } from '../environments/environment';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    FormsModule,
    ReactiveFormsModule,
    BrowserAnimationsModule,
    AngularFireModule.initializeApp(environment.firebase)
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

Any help would be highly appreciated


回答1:


You can create a function to receive the configuration:

import { environment } from '../environments/environment';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    FormsModule,
    ReactiveFormsModule,
    BrowserAnimationsModule,
    AngularFireModule.initializeApp(AppModule.getFirebaseConfig())
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
  static getFirebaseConfig(): FirebaseAppConfig {
    // do http request

    // return correct correct configuration depending on http request
    return environment.firebase;
  }
}


来源:https://stackoverflow.com/questions/46824209/firebase-angular-4-initialize-based-on-node-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!