最近,我开始学习如何将firebase的概念与angular结合使用。首先,我尝试让登录过程正常工作。目前,当我试图导航到登录页面时,出现了一个ennoying错误,我无法找出是什么导致了这个错误。我得到的错误是:
错误错误:未捕获(承诺中):错误:
静态注入错误(appmodule)[angularferauth->注入令牌
angularfer2.app.options]:静态注入错误(平台:
核心)[AngularFireAuth->InjectionToken AngularFire2.app.options]:
nullInjectorError:没有InjectionToken angularFire2.app.options的提供程序!
我需要做什么来解决这个错误?另外,我看到很多代码使用angularfire2而不是@angular/fire。这两个和我应该用哪一个有什么区别?
这是我目前掌握的代码:
应用模块.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { AngularFireModule } from '@angular/fire';
import { AngularFireDatabaseModule } from '@angular/fire/database';
import { environment } from 'environments/environment';
import * as firebase from 'firebase/app';

import { AppComponent } from './app.component';
import { FIREBASE_SERVICES } from './core/firebase/services';
import { AUTHENTICATION_GUARDS } from './features/authentication/guards';
import { AUTHENTICATON_ROUTES } from './features/authentication/authentication.route';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AngularFireAuthModule } from '@angular/fire/auth';
import { DashboardComponent } from './features/dashboard/dashboard/dashboard.component';
import { LogInComponent } from './features/authentication/login/login.component';
import { DASHBOARD_ROUTES } from './features/dashboard/dashboard.route';

firebase.initializeApp(environment.firebase);

@NgModule({
    declarations: [
        AppComponent,
        DashboardComponent,
        LogInComponent
    ],
    imports: [
        AngularFireModule,
        AngularFireDatabaseModule,
        AngularFireAuthModule,
        BrowserModule,
        RouterModule,
        FormsModule,
        ReactiveFormsModule,
        RouterModule.forRoot(AUTHENTICATON_ROUTES),
        RouterModule.forRoot(DASHBOARD_ROUTES)
    ],
    providers: [
        FIREBASE_SERVICES,
        AUTHENTICATION_GUARDS
    ],
    bootstrap: [AppComponent]
})
export class AppModule {
    constructor() {
        console.log("App module created");
    }
}

login.component.ts登录
import { Component } from '@angular/core';
import { Credentials } from '@app/core/firebase/models';
import { AuthenticationService } from '@app/core/firebase/services/authentication.service';
import { Router } from '@angular/router';

@Component({
    selector: 'app-log-in',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.less']
})
export class LogInComponent {
    emailaddress: string = '';
    password: string = '';

    constructor(private readonly _authenticationService: AuthenticationService,
        private readonly _router: Router) {
    }

    login() {
        console.log('log in clicked');

        const credentials = new Credentials(this.emailaddress, this.password);

        this._authenticationService.login(credentials)
            .then(
                () => this._router.navigate['/dashboard'],
                error => {
                    console.log(error);
                    alert(error.message);
                }
            );
    }
}

身份验证.service.ts
import { Injectable } from '@angular/core';
import { isNullOrUndefined } from 'util';
import { AngularFireAuth } from '@angular/fire/auth';
import * as firebase from 'firebase/app';

import { Credentials } from '@app/core/firebase/models';

@Injectable()
export class AuthenticationService {

    constructor(private readonly _angularFireAuthentication: AngularFireAuth) {
        console.log("Authentication Service created");
    }

    login(credentials: Credentials) {
        return new Promise((resolve, reject) => {
            this._angularFireAuthentication.auth
                .signInWithEmailAndPassword(credentials.emailaddress, credentials.password)
                .then(
                    result => resolve(result),
                    error => reject(error)
                );
        });
    }

    logout() {
        return new Promise((resolve, reject) => {
            if (this.isUserLoggedIn()) {
                this._angularFireAuthentication.auth.signOut();
                resolve();
            }
            else {
                reject();
            }
        });
    }

    private isUserLoggedIn(): boolean {
        return !isNullOrUndefined(firebase.auth().currentUser);
    }
}

pacakge.json中的依赖项部分
"dependencies": {
    "@angular/animations": "~7.1.0",
    "@angular/common": "~7.1.0",
    "@angular/compiler": "~7.1.0",
    "@angular/core": "~7.1.0",
    "@angular/forms": "~7.1.0",
    "@angular/platform-browser": "~7.1.0",
    "@angular/platform-browser-dynamic": "~7.1.0",
    "@angular/router": "~7.1.0",
    "core-js": "^2.5.4",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26",
    "firebase": "^5.5.5",
    "@angular/fire": "^5.0.2"
  },

最佳答案

你没有正确初始化应用程序。当导入AngularFireModule时,需要在此处初始化:

imports: [
    AngularFireModule.initializeApp(yourFirebaseConfig),
    AngularFireDatabaseModule,
    // ... the rest
],

更多的in the docs
它以前被称为angularfire2但在v5版本中,它们被移动到@angular范围。从这里开始,它是@angular/fire,而不是angularfire2.

07-24 09:46