pass angularFire config imported in library using

2019-04-16 07:02发布

问题:

I use angularFire2 in a custom library

@NgModule({
  imports: [
    CommonModule,
    AngularFireModule.initializeApp(firebaseConfig),
    AngularFirestoreModule
  ]
})
export class CustomModule {
  static forRoot(firebaseConfig: FirebaseOptions): ModuleWithProviders {
    return {
    ...
    }
  }
}

the consumer library call CustomModule.forRoot({config...})

My question is how do I make the config data available in AngularFireModule.initializeApp(firebaseConfig) ?

回答1:

I ran into this problem couple of weeks ago, what you have to do is drop the initializeApp call in the imports section, and add the FirebaseOptionsToken to your forRoot declaration like below:

@NgModule({
  imports: [
    CommonModule,
    AngularFireModule,
    AngularFirestoreModule
  ]
})
export class CustomModule {
  static forRoot(firebaseConfig: FirebaseOptions): ModuleWithProviders {
    return {
       ngModule: CustomModule,
       providers: [
           { provide: FirebaseOptionsToken, useValue: firebaseConfig }
       ]
    }
  }
}

If you look at the initializeApp method in Angular/Fire you see how it does the same thing when it is called.

It worked for me, hopefully will help others having the same issue.