2

I'm creating a library module (AuthModule), which depends on a third party module (AngularFire's AngularFireModule). My library (AuthModule) will be used inside a few other applications.

Thing is, to be able to import AngularFire I have to pass it some configurations, like this:

@NgModule({
    declarations: [ ... ],
    imports: [
        AngularFireModule.initializeApp(enviroment.firebaseOptions)
    ],
    providers: [...],
    exports: []
})
export class AuthModule { }

The problem is that I don't want to hardcode the options/configurations within my library, because these will vary depending on which application is using it.

I was thinking on using a forRoot method in my module in a way that it receives the config as a paramater and then uses it to initialize the third-party module (AngularFireModule).

But is it possible to import another module inside a forRoot method? How?

Thanks!

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Lucas Colombo
  • 513
  • 2
  • 5
  • 17

1 Answers1

0

You could use Angular's forRoot method and initialize the AngularFireModule there instead (this is for older versions of the Firebase SDK, as used in the question, newer versions have a different syntax).

AuthModule:

...
@NgModule({
  // do not initialize AngularFireModule here
  imports: [AngularFireModule, ...],
  declarations: [...],
  exports: [AngularFireModule, ...],
  providers: [...]
})
export class AuthModule {
   static forRoot(firebaseOptions: any): ModuleWithProviders<AuthModule> {

   [...]
   // initialize AngularFireModule here
   AngularFireModule.initializeApp(firebaseOptions)

  return {
    ngModule: AuthModule,
    providers: [...]
  };
 }

 static forFeature(): Type<AuthModule> {
   return AuthModule;
 }

}

AppModule:

@NgModule({
declarations: [ ... ],
imports: [
    // your module here with the firebase info
    AuthModule.forRoot(enviroment.firebaseOptions)
],
providers: [...],
exports: []
})

export class AppModule { }
jparg
  • 1,138
  • 8
  • 9