【问题标题】:Angular 12: Firebase modulesnot correctly provided (?)Angular 12:未正确提供 Firebase 模块(?)
【发布时间】:2021-12-18 20:35:20
【问题描述】:

第一次使用 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).

我错过了什么?

【问题讨论】:

  • 你检查过this吗?
  • @ApoorvaChikara 同样的事情
  • 尝试将AngularFireModule 添加到您的提供程序,并将初始化程序更改为AngularFireModule.initializeApp(youConfig)。请检查这是否有效。这些是由import { AngularFireModule } from '@angular/fire'; 导入的
  • 也不行
  • 我遇到了完全相同的问题,我找不到任何文档或问题,compat 变体有效,但 ios 问题只能通过不使用 compat 版本来解决:/

标签: angular firebase firebase-authentication


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多