属性身份验证在类型上不存在 AngularFireAuth:找不到名称'auth'(错误:T2304)



我有一个问题,正在实现"Firebase与Google的身份验证";在我的angular 8应用程序中。似乎";auth";自@angular/fire的6.0.0版本以来,根本不支持属性工作!!!

以下是我的依赖项:

"角度/cdk":"11.0.1";,"角/火":"6.1.4";,"firebase":"8.1.2";,

我的登录服务:(Login.service.ts(:

import { Injectable } from '@angular/core';
// import { auth } from 'firebase/app';
import { AngularFireAuth } from "@angular/fire/auth";

@Injectable({
providedIn: 'root'
})
export class LoginService {
constructor(
public afAuth: AngularFireAuth, // Inject Firebase auth service
) { }
// Sign in with Google
GoogleAuth() {
return this.AuthLogin(new auth.GoogleAuthProvider());
}  
// Auth logic to run auth providers
AuthLogin(provider: any) {
return this.afAuth.signInWithPopup(provider)
.then((result: any) => {
console.log('You have been successfully logged in!')
}).catch((error: any) => {
console.log(error)
})
}
}

我的登录组件.ts:

import { Component, OnInit } from '@angular/core';
import { LoginService } from '../../services/login.service';

@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
constructor(public loginService: LoginService) { }
ngOnInit() { }
}

我的登录组件.html:

<button mat-raised-button  (click)="LoginService.GoogleAuth()" color="primary" >Login with Google</button>

有人能帮我解决这个问题吗?感谢您对的帮助

安装firebase

npm i firebase --save

将firebase导入为:

import * as firebase from 'firebase';

然后将其用作

return firebase.auth().signInWithPopup((new firebase.auth.GoogleAuthProvider()));

我遇到了同样的问题,特别是这部分:

// Sign in with Google
GoogleAuth() {
return this.AuthLogin(new auth.GoogleAuthProvider());
} 

我按照这里的说明解决了这个问题:https://github.com/angular/angularfire/blob/master/docs/auth/getting-started.md

简而言之,我删除了{ auth }导入(就像您所做的那样(,改为导入firebase

// import { auth } from 'firebase/app';
import firebase from 'firebase/app';

现在您可以将上述部分的代码转换如下:

// Sign in with Google
GoogleAuth() {
return this.AuthLogin(new firebase.auth.GoogleAuthProvider());
}

请记住,firebase导入应该是import firebase from 'firebase/app';,而不是import * as firebase from 'firebase/app';,但我真的不知道为什么。

最新更新