我创建了一个服务文件来管理我的Firebase用户身份验证。但是,我在auth.service.ts文件中找到了导入错误。下面是错误,后面是导入以及ts文件中的代码。
错误:
src / app / core / auth.service.ts(2,10)中的错误:错误TS2305:模块'“ C:/ Users / Zakariah Siyaji / Desktop / or-api / node_modules / angularfire2 / index”已具有没有导出的成员“ AngularFireAuth”。 src / app / core / auth.service.ts(2,27):错误TS2305:模块'“ C:/ Users / Zakariah Siyaji / Desktop / or-api / node_modules / angularfire2 / index”'没有导出的成员'AngularFireDatabase '。 src / app / core / auth.service.ts(2,48):错误TS2305:模块'“ C:/ Users / Zakariah Siyaji / Desktop / or-api / node_modules / angularfire2 / index”'没有导出的成员'FirebaseAuthState '。 src / app / core / auth.service.ts(2,67):错误TS2305:模块'“ C:/ Users / Zakariah Siyaji / Desktop / or-api / node_modules / angularfire2 / index”'没有导出的成员AuthProviders '。 src / app / core / auth.service.ts(2,82):错误TS2305:模块'“ C:/ Users / Zakariah Siyaji / Desktop / or-api / node_modules / angularfire2 / index”'没有导出的成员AuthMethods '。 src / app / core / auth.service.ts(2,95):错误TS2305:模块'“ C:/ Users / Zakariah Siyaji / Desktop / or-api / node_modules / angularfire2 / index”'没有导出的成员'AngularFire '。
我「wdm」:编译失败。
进口:
从“ angularfire2”导入{AngularFireAuth,AngularFireDatabase,FirebaseAuthState,AuthProviders,AuthMethods,AngularFire};
angularfire2也可以在package.json文件夹中找到,其版本号为:“ ^ 5.0.0-rc.11”
代码:
import { Injectable } from '@angular/core';
import { AngularFireAuth, AngularFireDatabase, FirebaseAuthState, AuthProviders, AuthMethods, AngularFire } from "angularfire2";
import { Router } from "@angular/router";
@Injectable({
providedIn: 'root'
})
export class AuthService {
authState: FirebaseAuthState = null;
constructor(private af: AngularFire, private db: AngularFireDatabase, private router: Router) {
af.auth.subscribe((auth) => {
this.authState = auth;
});
}
//Return true if user is logged in
get authenticated(): boolean {
return this.authState !== null;
}
//Returns current user
get currentUser(): any {
return this.authenticated ? this.authState.auth : null;
}
//Returns current user UID
get currentUserId(): string {
return this.authenticated ? this.authState.uid : '';
}
// Anonymous User
get currentUserAnonymous(): boolean {
return this.authenticated ? this.authState.Anonymous : false
}
//Returns current user display name or guest
get currentUserDisplayName(): string {
if (!this.authenticated) { return 'GUEST' }
else if (this.currentUserAnonymous) { return 'ANONYMOUS'}
else { return this.authState.auth.displayName || 'OAUTH USER'}
}
//login through a Gmail account
googleLogin(): Promise<FirebaseAuthState> {
return this.socialSignIn(AuthProviders.Google);
}
private socialSignIn(provider: number): Promise<FirebaseAuthState> {
return this.af.auth.login({provider, method: AuthMethods.popup})
.then(() => this.updateUserData() )
.catch(error => console.log(error));
}
private updateUserData(): void {
//Writes user name and email to realtime db
//useful if your app displays information about users or for admin features
let path = `users/${this.currentUserId}`; //Endpoint on Firebase
let data = { name: this.currentUser.displayName, email: this.currentUser.email, }
this.db.object(path).update(data)
.catch(error => console.log(error));
}
}
答案 0 :(得分:0)
错误消息非常清楚。您从错误的路径导入。下面是一些正确的示例,请查看导入位置。
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase } from 'angularfire2/database';
答案 1 :(得分:0)
您只需要更新导入语句。例如,要导入auth和db模块,应执行以下操作:
import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule, AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import { AngularFireAuthModule, AngularFireAuth } from 'angularfire2/auth';
文档给出了full upgrade path。