Angular Fire - 没有 InjectionToken 提供程序 (angularfire2.app.options)
Angular Fire - No Provider for InjectionToken (angularfire2.app.options)
上下文
我正在使用 Ionic & Angular & Angular Fire & Firebase。我已经与 Firestore 数据库建立了成功连接,并且我能够操作数据。
规格
Ionic CLI : 6.18.1
Ionic Framework : @ionic/angular 6.0.11
@angular-devkit/build-angular : 13.2.6
@angular-devkit/schematics : 13.2.6
@angular/cli : 13.2.6
@ionic/angular-toolkit : 6.1.0
@angular/angularfire : 7.2.1
实施
App.module.ts
import { environment } from '../environments/environment';
import { initializeApp, provideFirebaseApp } from '@angular/fire/app';
import { provideAuth, getAuth } from '@angular/fire/auth';
import { provideFirestore, getFirestore } from '@angular/fire/firestore';
@NgModule({
...
imports: [
...
provideFirebaseApp(
() => initializeApp(environment.firebaseConfig)),
provideAuth(() => getAuth()),
provideFirestore(() => getFirestore(),
),
],
...
})
现在我正在致力于 Google 身份验证 的集成。为此,我一直在关注 Angular Fire Repository and of course the Angular Fire - Authentication Documentation。实现内容的缩写在哪里(就像文档一样):
Login.page.ts
import { AuthenticationService } from 'src/app/services/authentication.service';
export class LoginPage implements OnInit {
constructor(private authenticationSvc: AuthenticationService) { }
login() { this.authenticationSvc.login(); }
}
Authentication.service.ts
import { AngularFireAuth } from '@angular/fire/compat/auth';
import firebase from 'firebase/compat/app';
export class AuthenticationService {
constructor(private auth: AngularFireAuth) { }
login() { this.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider()); }
}
问题
简单地说,如果我在 Login.page.ts 上使用 Authentication.service.ts,应用程序将不再工作.浏览器控制台输出如下错误:
ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(LoginPageModule)[AuthenticationService -> AuthenticationService -> AngularFireAuth -> InjectionToken angularfire2.app.options -> InjectionToken angularfire2.app.options -> InjectionToken angularfire2.app.options]:
NullInjectorError: No provider for InjectionToken angularfire2.app.options!
NullInjectorError: R3InjectorError(LoginPageModule)[AuthenticationService -> AuthenticationService -> AngularFireAuth -> InjectionToken angularfire2.app.options -> InjectionToken angularfire2.app.options -> InjectionToken angularfire2.app.options]:
NullInjectorError: No provider for InjectionToken angularfire2.app.options!
at NullInjector.get (core.mjs:11120:1)
at R3Injector.get (core.mjs:11287:1)
at R3Injector.get (core.mjs:11287:1)
at R3Injector.get (core.mjs:11287:1)
at injectInjectorOnly (core.mjs:4765:1)
at Module.ɵɵinject (core.mjs:4769:1)
at Object.AngularFireAuth_Factory [as factory] (angular-fire-compat-auth.js:126:1)
at R3Injector.hydrate (core.mjs:11457:1)
at R3Injector.get (core.mjs:11276:1)
at injectInjectorOnly (core.mjs:4765:1)
at resolvePromise (zone.js:1262:1)
at resolvePromise (zone.js:1216:1)
at zone.js:1329:1
at _ZoneDelegate.push.6084._ZoneDelegate.invokeTask (zone.js:443:1)
at Object.onInvokeTask (core.mjs:25535:1)
at _ZoneDelegate.push.6084._ZoneDelegate.invokeTask (zone.js:442:1)
at Zone.push.6084.Zone.runTask (zone.js:214:1)
at drainMicroTaskQueue (zone.js:632:1)
解决方案
在文件 App.component.ts 中添加以下提供程序对象:
import { FIREBASE_OPTIONS } from '@angular/fire/compat';
@NgModule({
...
providers: [
{ provide: FIREBASE_OPTIONS, useValue: environment.firebaseConfig }
],
})
所有道具都转到@Saumya 回答这个question。
上下文
我正在使用 Ionic & Angular & Angular Fire & Firebase。我已经与 Firestore 数据库建立了成功连接,并且我能够操作数据。
规格
Ionic CLI : 6.18.1
Ionic Framework : @ionic/angular 6.0.11
@angular-devkit/build-angular : 13.2.6
@angular-devkit/schematics : 13.2.6
@angular/cli : 13.2.6
@ionic/angular-toolkit : 6.1.0
@angular/angularfire : 7.2.1
实施
App.module.ts
import { environment } from '../environments/environment';
import { initializeApp, provideFirebaseApp } from '@angular/fire/app';
import { provideAuth, getAuth } from '@angular/fire/auth';
import { provideFirestore, getFirestore } from '@angular/fire/firestore';
@NgModule({
...
imports: [
...
provideFirebaseApp(
() => initializeApp(environment.firebaseConfig)),
provideAuth(() => getAuth()),
provideFirestore(() => getFirestore(),
),
],
...
})
现在我正在致力于 Google 身份验证 的集成。为此,我一直在关注 Angular Fire Repository and of course the Angular Fire - Authentication Documentation。实现内容的缩写在哪里(就像文档一样):
Login.page.ts
import { AuthenticationService } from 'src/app/services/authentication.service';
export class LoginPage implements OnInit {
constructor(private authenticationSvc: AuthenticationService) { }
login() { this.authenticationSvc.login(); }
}
Authentication.service.ts
import { AngularFireAuth } from '@angular/fire/compat/auth';
import firebase from 'firebase/compat/app';
export class AuthenticationService {
constructor(private auth: AngularFireAuth) { }
login() { this.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider()); }
}
问题
简单地说,如果我在 Login.page.ts 上使用 Authentication.service.ts,应用程序将不再工作.浏览器控制台输出如下错误:
ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(LoginPageModule)[AuthenticationService -> AuthenticationService -> AngularFireAuth -> InjectionToken angularfire2.app.options -> InjectionToken angularfire2.app.options -> InjectionToken angularfire2.app.options]:
NullInjectorError: No provider for InjectionToken angularfire2.app.options!
NullInjectorError: R3InjectorError(LoginPageModule)[AuthenticationService -> AuthenticationService -> AngularFireAuth -> InjectionToken angularfire2.app.options -> InjectionToken angularfire2.app.options -> InjectionToken angularfire2.app.options]:
NullInjectorError: No provider for InjectionToken angularfire2.app.options!
at NullInjector.get (core.mjs:11120:1)
at R3Injector.get (core.mjs:11287:1)
at R3Injector.get (core.mjs:11287:1)
at R3Injector.get (core.mjs:11287:1)
at injectInjectorOnly (core.mjs:4765:1)
at Module.ɵɵinject (core.mjs:4769:1)
at Object.AngularFireAuth_Factory [as factory] (angular-fire-compat-auth.js:126:1)
at R3Injector.hydrate (core.mjs:11457:1)
at R3Injector.get (core.mjs:11276:1)
at injectInjectorOnly (core.mjs:4765:1)
at resolvePromise (zone.js:1262:1)
at resolvePromise (zone.js:1216:1)
at zone.js:1329:1
at _ZoneDelegate.push.6084._ZoneDelegate.invokeTask (zone.js:443:1)
at Object.onInvokeTask (core.mjs:25535:1)
at _ZoneDelegate.push.6084._ZoneDelegate.invokeTask (zone.js:442:1)
at Zone.push.6084.Zone.runTask (zone.js:214:1)
at drainMicroTaskQueue (zone.js:632:1)
解决方案
在文件 App.component.ts 中添加以下提供程序对象:
import { FIREBASE_OPTIONS } from '@angular/fire/compat';
@NgModule({
...
providers: [
{ provide: FIREBASE_OPTIONS, useValue: environment.firebaseConfig }
],
})
所有道具都转到@Saumya 回答这个question。