You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular+Firebase报错:类型typeof firebase不存在default属性

解决:Property 'default' does not exist on type 'typeof firebase' 错误

核心修复方案

直接移除代码中的.default调用即可,修改后的countViews方法如下:

countViews(postId: any){
    const viewCount= {
      views: firebase.firestore.FieldValue.increment(1) // 移除多余的.default
    }
    this.afs.doc(`posts/${postId}`).update(viewCount).then(() => {
      console.log("VIEWS updated");
    })
  }

原因说明

你使用的是Firebase兼容层(compat)的ES模块默认导入方式:import firebase from 'firebase/compat/app',此时导入的firebase对象本身就是兼容模式的完整实例,不需要额外访问.default属性。.default是旧CommonJS模块导出的遗留用法,在ES模块的默认导入场景下完全不需要。

另外你已经正确导入了firebase/compat/firestore,这会自动将Firestore的兼容API挂载到firebase对象上,确保firebase.firestore.FieldValue可以正常访问。

额外检查项

  • 确认tsconfig.json中的module配置为ES2020或更高版本,确保TypeScript正确处理ES模块导入。
  • 保持@angular/fire与Angular版本匹配(当前你的@angular/fire@7.6.1与Angular 14版本兼容,无需调整)。

内容的提问来源于stack exchange,提问作者notarobot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 22:22:19