我在我的Angular组件中使用Cloud Firestore,我插入了这些导入来执行:

import firebase from 'firebase/compat/app';
import 'firebase/compat/firestore';

然后我做了db = firebase.firestore()次,我可以做正常的Firestore操作.但是有一个组件,我得到了这个错误:

错误错误:未被捕获(promise ):FirebaseError: firebase 基地:尚未创建Firebase App "[Default]"—调用Firebase App. initializeApp()(app—compat/no—app)

我通过使用AngularFireModule. initializeApp()和Firebase配置值导入app. Module. ts来初始化Firebase.我还在app.module.ts中导入了AngularFireDatabaseModule、AngularFirestoreModule和AngularFireController.

我试过删除和重新制作组件.我也可以运行其他组件的Firestore代码,然后转到该组件,它仍然会给出错误,所以它不会在Firebase运行之前运行.

推荐答案

try 在AppModule中使用@ NgModule之前使用initializeApp

firebase.initializeApp(environment.firebase);

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [
    BrowserModule, 
    IonicModule.forRoot(), 
    AppRoutingModule,
    AngularFireAuthModule
  ],

Angular相关问答推荐

布局之间的Angular 变化检测需要重新加载

无法执行客户端功能Angular 17与ssr'

单元测试 case 模拟store 中的行为主体

垫-菜单未以17.2.3角显示

当项目具有BrowserAnimationsModule时,Angular UI未拾取数据模型更改

如何使用带有17角的Swiper 11.0.5元素

是否可以在Angular(12+)中迭代指定的范围而不是整个数组

使用指令的 Angular 自定义验证在 ng-change 中无法正常工作

我的验证器收到一个始终为空的可观察值

Angular 迁移 14 到 15 / 16:angular universal 是否停止将 <!-- this page was prerendered with angular universal --> 用于预渲染页面?

Angular ngSubmit 不起作用(内部按钮和导入模块)

Angular:组件的内容投影访问数据

Angular UNIVERSAL prerender 错误方法 Promise.prototype.then 调用不兼容的接收器 [object Object]

如何重置表单中的特定字段?

Angular CLI 自定义 webpack 配置

如何每隔一段时间发出 HTTP 请求?

为什么用?模板绑定中的运算符?

formGroup 需要一个 FormGroup 实例

No provider for Router?

如何升级 Angular CLI 项目?