属性'auth'在'AngularFireAuth'类型上不存在

问题描述 投票:1回答:1

使用angularfire时出现此错误。检查了我的进口货,它们似乎是正确的。我试图重新安装angularfire,但是它仍然抛出此错误。 angularfire是否有问题?

import { Injectable, NgZone } from '@angular/core';
import { User } from "../services/user";
import { auth } from 'firebase/app';
import { AngularFireAuth } from "@angular/fire/auth";
import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/firestore';
import { Router } from "@angular/router";


@Injectable({
  providedIn: 'root'
})

export class AuthService {
  userData: any; // Save logged in user data

  constructor(
    public afs: AngularFirestore,   // Inject Firestore service
    public afAuth: AngularFireAuth, // Inject Firebase auth service
    public router: Router,  
    public ngZone: NgZone // NgZone service to remove outside scope warning
  ) {    
    /* Saving user data in localstorage when 
    logged in and setting up null when logged out */
    this.afAuth.authState.subscribe(user => {
      if (user) {
        this.userData = user;
        localStorage.setItem('user', JSON.stringify(this.userData));
        JSON.parse(localStorage.getItem('user'));
      } else {
        localStorage.setItem('user', null);
        JSON.parse(localStorage.getItem('user'));
      }
    })
  }

  // Sign in with email/password
  SignIn(email, password) {
    return this.afAuth.auth.signInWithEmailAndPassword(email, password)
      .then((result) => {
        this.ngZone.run(() => {
          this.router.navigate(['dashboard']);
        });
        this.SetUserData(result.user);
      }).catch((error) => {
        window.alert(error.message)
      })
  }
angular firebase angularfire
1个回答
0
投票

我有同样的问题,这与package.json中的版本不匹配有关。我有

dependencies {
        ...
        "@angular/fire": "latest", // It was 6.0.0
        "firebase": "5.4.2",
        ...
}

然后我改为

dependencies {
        ...
        "@angular/fire": "5.4.2",
        "firebase": "5.4.2",
        ...
}

还请确保将AngularFireAuthModule导入您的app.module.ts

import { AngularFireAuthModule } from '@angular/fire/auth';

...
@NgModule({
    imports: [
        AngularFireAuthModule
    ],
    ....
})
export class AppModule { }
© www.soinside.com 2019 - 2024. All rights reserved.