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
相关产品推荐
相关产品推荐

