简体   繁体   English

Angular Firebase:导入问题

[英]Angular Firebase: issue with imports

I created a service file to manage my firebase user authentication. 我创建了一个服务文件来管理Firebase用户身份验证。 But, I am getting errors for the imports that are found in the auth.service.ts file. 但是,我在auth.service.ts文件中找到了导入错误。 Below is the error followed by the imports and the code from the ts file. 下面是错误,后面是导入以及ts文件中的代码。

ERRORS: 错误:

ERROR in src/app/core/auth.service.ts(2,10): error TS2305: Module '"C:/Users/Zakariah Siyaji/Desktop/or-api/node_modules/angularfire2/index"' has no exported member 'AngularFireAuth'. 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): error TS2305: Module '"C:/Users/Zakariah Siyaji/Desktop/or-api/node_modules/angularfire2/index"' has no exported member 'AngularFireDatabase'. 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): error TS2305: Module '"C:/Users/Zakariah Siyaji/Desktop/or-api/node_modules/angularfire2/index"' has no exported member 'FirebaseAuthState'. 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): error TS2305: Module '"C:/Users/Zakariah Siyaji/Desktop/or-api/node_modules/angularfire2/index"' has no exported member 'AuthProviders'. 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): error TS2305: Module '"C:/Users/Zakariah Siyaji/Desktop/or-api/node_modules/angularfire2/index"' has no exported member 'AuthMethods'. 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): error TS2305: Module '"C:/Users/Zakariah Siyaji/Desktop/or-api/node_modules/angularfire2/index"' has no exported member 'AngularFire'. src / app / core / auth.service.ts(2,95):错误TS2305:模块'“ C:/ Users / Zakariah Siyaji / Desktop / or-api / node_modules / angularfire2 / index”'没有导出的成员'AngularFire ”。

i 「wdm」: Failed to compile. 我「wdm」:编译失败。

Imports: 进口:

import { AngularFireAuth, AngularFireDatabase, FirebaseAuthState, AuthProviders, AuthMethods, AngularFire } from "angularfire2"; 从“ angularfire2”导入{AngularFireAuth,AngularFireDatabase,FirebaseAuthState,AuthProviders,AuthMethods,AngularFire};

angularfire2 is also found in the package.json folder with version number: "^5.0.0-rc.11" 在package.json文件夹中还可以找到angularfire2,版本号为:“ ^ 5.0.0-rc.11”

code: 码:

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));
}


}

The error messages are pretty clear. 错误消息非常清楚。 You import from wrong paths. 您从错误的路径导入。 Below are some correct examples, look at the import locations. 以下是一些正确的示例,请查看导入位置。

import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase } from 'angularfire2/database';

You just need to update your import statements. 您只需要更新您的导入语句。 For example, to import the auth and db modules, you should do: 例如,要导入auth和db模块,您应该执行以下操作:

import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule, AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import { AngularFireAuthModule, AngularFireAuth } from 'angularfire2/auth';

The documentation gives a full upgrade path . 该文档提供了完整的升级路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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