使用 forRoot 传递在库中导入的 angularFire 配置 [英] pass angularFire config imported in library using forRoot

查看:18
本文介绍了使用 forRoot 传递在库中导入的 angularFire 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义库中使用 angularFire2

I use angularFire2 in a custom library

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

消费者库调用 CustomModule.forRoot({config...})

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

我的问题是如何在 AngularFireModule.initializeApp(firebaseConfig) 中提供配置数据?

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

推荐答案

几周前我遇到了这个问题,你必须做的是在导入部分删除 initializeApp 调用,然后将 FirebaseOptionsToken 添加到您的 forRoot 声明中,如下所示:

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 }
       ]
    }
  }
}

如果您查看 initializeApp 方法 在 Angular/Fire 中,你会看到它在被调用时如何做同样的事情.

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.

这篇关于使用 forRoot 传递在库中导入的 angularFire 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆