简体   繁体   中英

NullInjectorError: No provider for AngularFireAnalytics! Angular 8

When run 'npm test', I receive this error:

NullInjectorError: StaticInjectorError(DynamicTestModule)[ComparePageComponent -> AngularFireAnalytics]: StaticInjectorError(Platform: core)[ComparePageComponent -> AngularFireAnalytics]: NullInjectorError: No provider for AngularFireAnalytics! error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'ComparePageComponent', Function ] })

app.module.ts

import {AngularFireModule} from "@angular/fire";
import {AngularFireAnalyticsModule, AngularFireAnalytics} from "@angular/fire/analytics";

@NgModule({
   imports: [
   .....
       AngularFireModule.initializeApp(environment.firebase),
       AngularFireAnalyticsModule,
   ],
   providers: [
      AngularFireAnalytics,
      ...fromServices.services,
   ],

firebase.service.ts

import { Injectable } from '@angular/core';
import { AngularFireAnalytics } from "@angular/fire/analytics";

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

    constructor(private analytics: AngularFireAnalytics) {}

      public logEvent(eventName: string, params: {}) {

          this.analytics.logEvent(eventName, params).then( res => {
             console.log('event registered', eventName, params);
          })
          .catch(error => {
             console.log('error', error.message);
          });
      }
}

compare-page.component.ts

import {Component, OnDestroy, OnInit} from '@angular/core';
import {FirebaseService} from "../../../services/firebase.service";

export class ComparePageComponent implements OnInit, OnDestroy {

constructor(private firebaseService: FirebaseService) {
     this.firebaseService.logEvent('test', {});
}

Do you have any idea what's missing?

I used this tutorial: https://www.positronx.io/firebase-authentication-in-angular-8-with-angularfire2/

There is no provider called AngularFireAnalytics . Therefore you need to change the providers array to the following:

  providers: [
      ...fromServices.services,
   ],

Then to use firebase analytics do the following in the component:

import { AngularFireAnalytics } from '@angular/fire/analytics';

constructor(analytics: AngularFireAnalytics) {
  analytics.logEvent('custom_event', { ... });
}

Check here :

https://github.com/angular/angularfire/blob/master/docs/analytics/getting-started.md

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM