Angular 12 : Firebase modulesnot correctly provided (? )

标签 angular firebase firebase-authentication

第一次使用 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 { }

在另一个模块中,使用延迟加载,我调用方法 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).

我错过了什么?

最佳答案

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

看来您需要在您的服务中注入(inject) 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 => { ... }
                }
}

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

关于 Angular 12 : Firebase modulesnot correctly provided (? ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69842218/

相关文章:

java - RecyclerView 不显示接收到的数据

firebase - 具有要在 Firebase 上设置的嵌套 map 的模型 (json_annotation.dart)

Swift Firebase 4.0-observeSingleEvent 不返回数据集(但身份验证正在工作)

Angular 异步重新加载微调器

angular - Angular 更新后 JIT 编译器不可用

javascript - 使用 Chrome 扩展内容脚本注入(inject) Angular 8 应用程序

ios - 从 'BaseViewController' 到 'FUIAuthDelegate' 的条件转换始终成功

android - FirebaseAuth.getInstance() 总是返回 null

ios - 无法在 iOS 上实现 Firebase Phone Auth

css - Angular 7 根据 JSON 列表中的值更改文本颜色