angular2firebase - 使用 Angular 6 的多个实例

angular2firebase - multiple instances using Angular 6

我正在使用 AngularFire2 升级到 Angular 6。我的应用程序引用了 2 Firebase 个项目,使用如下代码创建数据库引用:

public initFirebaseApp(config: FirebaseAppConfig, firebaseAppName: string) {
        this._db = new AngularFireDatabase(_firebaseAppFactory(config, firebaseAppName));
    }

此代码现已损坏。我明白了:

ERROR in src/app/services/firebase.service.ts(24,25): error TS2554: Expected 5 arguments, but got 1.

谢谢!

AngularFire 现在通过注入支持更多的配置对象,这就是它需要更多参数的原因。目前需要:

constructor(
  @Inject(FirebaseOptionsToken) options:FirebaseOptions,
  @Optional() @Inject(FirebaseNameOrConfigToken) nameOrConfig:string|FirebaseAppConfig|undefined,
  @Optional() @Inject(RealtimeDatabaseURL) databaseURL:string,
  @Inject(PLATFORM_ID) platformId: Object,
  zone: NgZone
)

虽然现在我们支持依赖注入,但我不建议像你支持多应用那样直接初始化。 We have an open issue for documenting this 但你现在可以通过 FirebaseOptionsToken 将不同的 FirebaseOptions 注入不同的组件,如果你需要在同一个组件中使用多个数据库,请使用这样的东西:

@Injectable()
export class AngularFireDatabaseAlpha extends AngularFireDatabase { }

@Injectable()
export class AngularFireDatabaseBeta extends AngularFireDatabase { }


export function AngularFireDatabaseAlphaFactory(platformId: Object, zone: NgZone): Project1AngularFireAuth {
  return new AngularFireDatabaseAlpha(environment.firebase[0], 'alpha', undefined, platformId, zone)
}
export function AngularFireDatabaseBetaFactory(platformId: Object, zone: NgZone): Project2AngularFireAuth {
  return new AngularFireDatabaseBeta(environment.firebase[1], 'beta', undefined, platformId, zone)
}


@NgModule({
  ...,
  providers: [
    ...,
    { provide: AngularFireDatabaseAlpha, deps: [PLATFORM_ID, NgZone], useFactory: AngularFireDatabaseAlphaFactory },
    { provide: AngularFireDatabaseBeta, deps: [PLATFORM_ID, NgZone], useFactory: AngularFireDatabaseBetaFactory },
    ...
  ],
  ...
})

然后你可以依靠依赖注入将 AngularFireDatabaseAlphaAngularFireDatabaseBeta 放入你的组件中。