简体   繁体   English

AngularFirestore 已经在 [DEFAULT] Firebase 应用程序上用不同的设置初始化

[英]AngularFirestore was already initialized on the [DEFAULT] Firebase App with different settings

So I have this small issue.所以我有这个小问题。

I have an Angular app and in the app.module.ts I initialize AngularFire我有一个 Angular 应用程序,在 app.module.ts 中我初始化了 AngularFire

imports: 
[...
    AngularFireModule.initializeApp(environment.firebaseConfig),
    AngularFireAuthModule,
    AngularFireFunctionsModule,
...]

Now I have multiple smaller modules, like for example LazyModule.现在我有多个较小的模块,例如 LazyModule。 This module is loaded in app-routing.module.ts like so:这个模块像这样加载到 app-routing.module.ts 中:

{
    path: 'lazyModule',
    loadChildren: () => import('./modules/lazyModule/lazyModule.module').then(m => m.LazyModule)
},

Inside this LazyModule I do not import AngularFire again and I have a LazyComponent which requires the AngularFirestore在这个 LazyModule 中,我不再导入 AngularFire ,我有一个需要 AngularFirestore 的 LazyComponent

export class LazyComponent {
  constructor(private firestore: AngularFirestore){}
}

For some reason, each time I navigate to this component, AngularFire tries to reinitialize and throws this error:出于某种原因,每次我导航到该组件时,AngularFire 都会尝试重新初始化并抛出此错误:

AngularFirestore was already initialized on the [DEFAULT] Firebase App with different settings.

Why does this happen?为什么会这样?

Whenever you import AngularFire, it apparently caches an object containing the current submodule's configuration in globalThis.ɵAngularfireInstanceCache .每当您导入 AngularFire 时,它显然会在 globalThis.ɵAngularfireInstanceCache 中缓存一个包含当前子模块配置的globalThis.ɵAngularfireInstanceCache If you import the same submodule again, it checks the current instance's configuration against the cached configuration.如果您再次导入相同的子模块,它会根据缓存的配置检查当前实例的配置。 (The same submodule might be imported if multiple Angular child modules rely on AngularFire.) If the configurations don't match, then this error appears: [AngularFire submodule] was already initialized on the [DEFAULT] Firebase App with different settings. (如果多个 Angular 子模块依赖 AngularFire,可能会导入相同的子模块。)如果配置不匹配,则会出现此错误: [AngularFire submodule] was already initialized on the [DEFAULT] Firebase App with different settings. See this code .请参阅此代码

I know that the original question says that AngularFire was loaded in app.module , and the LazyModule did not import AngularFire.我知道最初的问题是说 AngularFire 是在app.module中加载的,而LazyModule没有导入 AngularFire。 A tip about that:关于这一点的提示:

Look for Angular services that depend on AngularFire.查找依赖于 AngularFire 的 Angular 服务。 Depending on where they are providedIn , they might be loaded in modules that otherwise don't involve AngularFire, and that are missing configuration defined elsewhere.根据providedIn它们的位置,它们可能会加载到不涉及 AngularFire 的模块中,并且缺少其他地方定义的配置。 This is very easy to miss.这是很容易错过的。

For example, I was configuring Auth's USE_EMULATOR in a child module, and using Auth with no configuration in an Angular guard in the main module.例如,我在子模块中配置 Auth 的USE_EMULATOR ,并在主模块的 Angular 守卫中使用 Auth 而没有配置。 The child module loaded with no errors, but the error was appearing for me upon navigation whenever the guard was involved.子模块加载没有错误,但每当涉及守卫时,导航时都会出现错误。 When the guard's dependencies were imported, a new instance of AngularFire Auth would be initialized, but was created with a different configuration than the one first cached for the child module.当守卫的依赖项被导入时,一个新的 AngularFire Auth 实例将被初始化,但创建时使用的配置与第一个为子模块缓存的配置不同。

I fixed it by injecting all AngularFire configuration in the main module.我通过在主模块中注入所有 AngularFire 配置来修复它。 Child modules apparently inherit things injected at the main module level.子模块显然继承了在主模块级别注入的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有 Firebase 应用程序“[默认]”错误,但 firebase 已经初始化 - No Firebase App '[Default]' error but firebase is already initialized 检查 Firebase 应用程序是否已在 python 中初始化 - check if a Firebase App is already initialized in python 如何检查 Firebase 应用程序是否已在 Android 上初始化 - How to check if a Firebase App is already initialized on Android Firebase 名为“[DEFAULT]”的应用已存在 REACT - Firebase App named '[DEFAULT]' already exists REACT Firebase 名为“[DEFAULT]”的 App 已经存在 - A Firebase App named "[DEFAULT]" already exists Google Cloud Functions Firebase 错误 默认的 Firebase 应用已经存在 - Google Cloud Functions Firebase Error The default Firebase app already exists 初始化 Firebase 后出现“无默认应用”异常 - 'no default app' exception after Firebase has been initialized 没有创建 Firebase App '[DEFAULT]' - 尽管正在初始化 Firebase,但调用 Firebase.initializeApp()) - No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()) although Firebase is being initialized 使用 Firebase 管理员 SDK,无法部署 Firebase 跨多个文件拆分的云功能:“默认 Firebase 应用程序已存在” - Using Firebase Admin SDK, Unable to Deploy Firebase Cloud Functions Split Across Multiple Files: "The default Firebase app already exists" 没有 firebase.app.App 实例当前在 Angular Application 中初始化 - No firebase.app.App instance is currently initialized in Angular Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM