Angular 12:Firebase 模块未正确提供(?)

Igi*_*ffa 5 firebase firebase-authentication angular

第一次使用 Firebase,所以我不知道发生了什么。我没有修改使用的配置ng add @angular/fire,所以我的配置AppModule是:

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    provideFirebaseApp(() => initializeApp(environment.firebase)),
    provideFirestore(() => getFirestore()),
    provideAuth(() => getAuth()),
    provideDatabase(() => getDatabase()),
    provideFunctions(() => getFunctions()),
    provideMessaging(() => getMessaging()),
    provideRemoteConfig(() => getRemoteConfig()),
    provideStorage(() => getStorage()),
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

在另一个模块中,使用延迟加载,我调用 method signInWithEmailAndPassword(auth, email, password),但在控制台中收到以下错误:

Either AngularFireModule has not been provided in your AppModule (this can be done manually or implictly using
provideFirebaseApp) or you're calling an AngularFire method outside of an NgModule (which is not supported).
Run Code Online (Sandbox Code Playgroud)

我缺少什么?

Nie*_*ren 11

好的,我找到了这个问题的解决方案。

看来您需要在服务中注入 angularfire 的 auth 实例。

import {Auth, sendPasswordResetEmail, signOut} from '@angular/fire/auth';

@Injectable({
    providedIn: 'root'
})
@Injectable()
export class FirebaseAuthService {

    constructor(..., private auth: Auth,) {
    }
    
     getFirebaseUser(): any {
        return this.auth.currentUser;
    }

    logout() {
        signOut(this.auth);
    }
    
    resetPassword(email: string) {
        return sendPasswordResetEmail(this.auth, email);
    }

    if (this.credentialsForm.valid) {
            signInWithEmailAndPassword(this.auth,this.credentialsForm.controls.email.value,
                this.credentialsForm.controls.password.value).then(result => { ... }
                }
}
Run Code Online (Sandbox Code Playgroud)

使用这个实例后,它就像一个魅力。