Angular/TypeScript语法错误排查:基于Angular与Firestore的登录系统问题
Hey there! Let's break down your code snippet to spot Angular/TypeScript syntax issues and common gotchas:
User Interface Syntax Error Your User interface is cut off mid-property definition:
interface User { uid: string; email: string; photoURL?: string; dis...
This is a clear syntax error—TypeScript requires interfaces to have complete, properly formatted properties. For example, if you meant displayName, finish it like this:
interface User { uid: string; email: string; photoURL?: string; displayName?: string; // Complete the property name // Add any other user fields you need here }
Don't forget to close the interface with a curly brace } too!
Your RxJS imports are for RxJS 5, but modern Angular (v6+) uses RxJS 6+. This will cause compilation errors:
// Old RxJS 5 syntax (error-prone in Angular 6+) import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/switchMap';
Update them to RxJS 6+ standards:
// Correct RxJS 6+ imports import { Observable, of } from 'rxjs'; import { switchMap } from 'rxjs/operators';
Also, when using operators like switchMap, you'll need to wrap them in the .pipe() method instead of chaining directly (more on that in the class structure below).
Your code stops after imports and the partial interface—you need a complete service class with the @Injectable() decorator and class body. Here's a corrected, minimal version:
@Injectable({ providedIn: 'root' // Best practice for Angular 6+ (no need to declare in NgModule) }) export class AuthService { user$: Observable<User | null>; constructor( private afAuth: AngularFireAuth, private afs: AngularFirestore, private router: Router ) { // Listen to auth state and fetch user data from Firestore this.user$ = this.afAuth.authState.pipe( switchMap(user => { if (user) { return this.afs.doc<User>(`users/${user.uid}`).valueChanges(); } else { return of(null); // Use `of` instead of Observable.of in RxJS 6+ } }) ); } // Example login method async signIn(email: string, password: string) { try { const credential = await this.afAuth.auth.signInWithEmailAndPassword(email, password); return this.updateUserData(credential.user); } catch (error) { console.error('Login error:', error); throw error; } } private updateUserData(user: firebase.User) { const userRef: AngularFirestoreDocument<User> = this.afs.doc(`users/${user.uid}`); const userData: User = { uid: user.uid, email: user.email, photoURL: user.photoURL, displayName: user.displayName }; return userRef.set(userData, { merge: true }); } }
Without a complete class definition, TypeScript will throw syntax errors about missing class declarations.
firebase Type Compatibility Note If you're using a newer Firebase SDK (v9+), the import * as firebase from 'firebase/app'; syntax still works, but angularfire2 is a legacy package—consider migrating to @angular/fire (the official, maintained Angular Firebase library) for long-term compatibility. That said, this isn't a syntax error, just a best practice.
内容的提问来源于stack exchange,提问作者Prometheus

