应该清楚我在这里要实现的目标:
import { NgModule, ModuleWithProviders, InjectionToken } from '@angular/core'
import { AngularFireModule } from '@angular/fire'
import { AngularFireAuthModule } from '@angular/fire/auth'
import { AngularFirestoreModule } from '@angular/fire/firestore'
import { environment } from './environments/environment'
interface FirebaseConfig {
apiKey: string,
authDomain: string,
databaseURL: string,
projectId: string,
storageBucket: string,
messagingSenderId: string,
}
const FirebaseConfigToken = new InjectionToken<FirebaseConfig>('firebaseConfigToken')
@NgModule({
declarations: [
],
imports: [
AngularFireModule.initializeApp(environment.firebase, ''),
AngularFireAuthModule,
AngularFirestoreModule,
],
exports: [
AngularFireModule,
AngularFireAuthModule,
AngularFirestoreModule,
]
})
export class AfAuthModule {
public static forRoot(firebaseConfig: FirebaseConfig): ModuleWithProviders {
return {
ngModule: AfAuthModule,
providers: [
{ provide: FirebaseConfigToken, useValue: firebaseConfig },
]
}
}
}
目标是使用传递到模块的firebase配置(而不是使用文件顶部导入的环境)配置AngularFireModule。 我在正确的轨道上吗?与此相关的是,如何使用模块使用的forRoot()配置服务。
您可以看到,在imports数组中,我正在使用firebase env初始化应用程序(正常方式),但是在将模块导入最终的app.module.ts中时,我需要进行某种配置:
import { BrowserModule } from '@angular/platform-browser'
import { NgModule } from '@angular/core'
import { AppComponent } from './app.component'
import { AfAuthModule } from 'projects/af-auth/src/public-api'
import { environment } from '../environments/environment'
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AfAuthModule.forRoot(environment.firebase),
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }