pass angularFire config imported in library using

2019-04-16 06:21发布

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条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-16 07:27

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.

查看更多
登录 后发表回答