启动AngularCLI集成Firebase时遇TS错误,如何解决?
Hey there, let's work through these errors you're hitting when setting up AngularFire2 with Firebase—they almost always boil down to version mismatches between the two libraries. Here's how to fix them step by step:
Root Cause
These errors (missing firestore property in FirebaseApp, no exported firebase.Promise) happen because:
- Newer Firebase SDKs removed the custom
firebase.Promisein favor of native ES Promises - The
FirebaseAppinterface was updated in newer Firebase versions to includefirestore, but older AngularFire2 versions don't expect this property - In general, AngularFire2 (or its newer iteration
@angular/fire) needs to be paired with a specific Firebase SDK version to avoid interface mismatches
Step 1: Uninstall Current Versions
First, remove the incompatible packages from your project:
npm uninstall angularfire2 firebase --save
Step 2: Install Compatible Version Pairs
Choose the right combination based on your Angular version:
For Angular 6-7:
Stick with AngularFire2 and a matching Firebase 5.x version—this pair is well-tested and avoids the interface issues you're seeing:
npm install angularfire2@5.4.2 firebase@5.11.1 --save
For Angular 8+:
AngularFire2 was rebranded to @angular/fire—use the latest compatible versions here. Make sure @angular/fire matches your Angular major version (e.g., Angular 15 works with @angular/fire@7.5.x):
npm install @angular/fire firebase@7.x --save
Step 3: Verify Your App Module Configuration
Double-check your app.module.ts to ensure you're using the correct imports for your chosen library version:
For AngularFire2 (5.x):
import { AngularFireModule } from 'angularfire2'; import { AngularFireDatabaseModule } from 'angularfire2/database'; import { AngularFireAuthModule } from 'angularfire2/auth'; // Your Firebase project config from the Firebase console const firebaseConfig = { apiKey: "YOUR_API_KEY", authDomain: "YOUR_AUTH_DOMAIN", databaseURL: "YOUR_DATABASE_URL", projectId: "YOUR_PROJECT_ID", storageBucket: "YOUR_STORAGE_BUCKET", messagingSenderId: "YOUR_MESSAGING_SENDER_ID" }; @NgModule({ imports: [ AngularFireModule.initializeApp(firebaseConfig), AngularFireDatabaseModule, AngularFireAuthModule ] }) export class AppModule { }
For @angular/fire (7.x+):
import { AngularFireModule } from '@angular/fire'; import { AngularFireDatabaseModule } from '@angular/fire/database'; import { AngularFireAuthModule } from '@angular/fire/auth'; const firebaseConfig = { /* Your Firebase config */ }; @NgModule({ imports: [ AngularFireModule.initializeApp(firebaseConfig), AngularFireDatabaseModule, AngularFireAuthModule ] }) export class AppModule { }
Step 4: Clean Cache & Dependencies (If Needed)
If you still see errors after reinstalling, clear your npm cache and rebuild dependencies to eliminate any lingering conflicts:
rm -rf node_modules package-lock.json npm cache clean --force npm install
That should resolve the type mismatches and get your Angular + Firebase integration up and running smoothly!
内容的提问来源于stack exchange,提问作者imnickvaughn

