在Angular 13项目中添加firebase不起作用

发布于 2025-02-13 21:25:55 字数 3300 浏览 1 评论 0原文

我仍在尝试在一个Angular项目中使用Firebase。这是您的AppModule中未提供的错误“ 尚未提供AngularFiremodule(可以手动或暗示使用此操作 supportfirebaseapp)否则您正在调用ngmodule之外的AngularFire方法(不支持)

在开始之前,这是我的Angular-cli版本:

ng --version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 13.0.4
Node: 14.19.0
Package Manager: npm 6.14.16
OS: win32 x64

Angular: 13.0.3
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1300.4
@angular-devkit/build-angular   13.0.4
@angular-devkit/core            13.0.4
@angular-devkit/schematics      13.0.4
@angular/cli                    13.0.4

这是这些步骤,我正在关注:

  • Run run NG新的Angular-fire& amp; cd Angular-Fire
  • 我在firebase中创建了一个项目( https://console.firebase.google.com )并通过Google
  • Run Run ng添加 @ Angular/Fire(请仅选择选项身份验证。我们目前不需要其他
  • 命令 )已经更新了您的 app.module.ts ,并且环境文件

我的app.module.ts现在看起来像:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';
import { initializeApp,provideFirebaseApp } from '@angular/fire/app';
import { environment } from '../environments/environment';
import { provideAuth,getAuth } from '@angular/fire/auth';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    provideFirebaseApp(() => initializeApp(environment.firebase)),
    provideAuth(() => getAuth())
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

添加一个按钮:

<div>
  <button (click)="signIn()">Sign in with Google</button>
</div>

现在,在app.component.html中删除所有内容,然后 。

import { Component } from '@angular/core';
import {
  getAuth,
  GoogleAuthProvider,
  signInWithPopup,
} from '@angular/fire/auth';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.sass'],
})
export class AppComponent {
  title = 'angular-fire';

  signIn() {
    return signInWithPopup(getAuth(), new GoogleAuthProvider())
      .then((result) => {
        const user = result.user;
        console.log(user);
      })
      .catch((error) => {
        const errorCode = error.code;
        const errorMessage = error.message;

        console.log('errorCode', errorCode);
        console.log('errorMessage', errorMessage);
      });
  }
}

  • 您会看到此错误错误:您的AppModule中尚未提供AngularFiremodule(可以使用SubliceFireBaseApp手动或隐含地进行此操作),或者您在NGMODULE之外称呼AngularFire方法(不支持)。

事先感谢您的帮助!

I'm still trying to use firebase in an Angular project. Here is the error "Either AngularFireModule has not been provided in your AppModule (this can be done manually or implictly using
provideFirebaseApp) or you're calling an AngularFire method outside of an NgModule (which is not supported)
"

Before starting, here is my Angular-cli version:

ng --version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 13.0.4
Node: 14.19.0
Package Manager: npm 6.14.16
OS: win32 x64

Angular: 13.0.3
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1300.4
@angular-devkit/build-angular   13.0.4
@angular-devkit/core            13.0.4
@angular-devkit/schematics      13.0.4
@angular/cli                    13.0.4

Theses are the steps, I'm following :

  • Run ng new angular-fire && cd angular-fire
  • I've created a project in Firebase (https://console.firebase.google.com) and activated authentication with Google
  • Run ng add @angular/fire (Please, select only the option Authentication. We don't need the others for the moment)
  • the previous command has updated your app.module.ts, and environment files

my app.module.ts now looks like :

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';
import { initializeApp,provideFirebaseApp } from '@angular/fire/app';
import { environment } from '../environments/environment';
import { provideAuth,getAuth } from '@angular/fire/auth';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    provideFirebaseApp(() => initializeApp(environment.firebase)),
    provideAuth(() => getAuth())
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Now, delete everything in app.component.html and add just a button :

<div>
  <button (click)="signIn()">Sign in with Google</button>
</div>

and in app.component.ts, add signIn function :

import { Component } from '@angular/core';
import {
  getAuth,
  GoogleAuthProvider,
  signInWithPopup,
} from '@angular/fire/auth';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.sass'],
})
export class AppComponent {
  title = 'angular-fire';

  signIn() {
    return signInWithPopup(getAuth(), new GoogleAuthProvider())
      .then((result) => {
        const user = result.user;
        console.log(user);
      })
      .catch((error) => {
        const errorCode = error.code;
        const errorMessage = error.message;

        console.log('errorCode', errorCode);
        console.log('errorMessage', errorMessage);
      });
  }
}

Finally,

  • run ng serve -o
  • open the console of your browser
  • click on the button Sign in with Google
  • you'll see this error Error: Either AngularFireModule has not been provided in your AppModule (this can be done manually or implictly using provideFirebaseApp) or you're calling an AngularFire method outside of an NgModule (which is not supported).

Thanks in advance for your help !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

埋情葬爱 2025-02-20 21:25:55

现在正在工作。
首先,我必须使用npm I Firebase安装“ firebase”
其次,我现在使用了angularFireModuleangularFireAuthModule从“ compat”中删除了我现在使用的所有内容。 请参阅代码并注意

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';
import { AngularFireModule } from '@angular/fire/compat';
import { AngularFireAuthModule } from '@angular/fire/compat/auth';
import { environment } from '../environments/environment';

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFireAuthModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

在App.component.ts中,

import { Component } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/compat/auth';
import * as auth from 'firebase/auth';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.sass'],
})
export class AppComponent {
  title = 'angular-fire';

  constructor(private afAuth: AngularFireAuth) {}

  signIn() {
    return this.afAuth
      .signInWithPopup(new auth.GoogleAuthProvider())
      .then((result) => {
        const user = result.user;
        console.log(user);
      })
      .catch((error) => {
        const errorCode = error.code;
        const errorMessage = error.message;

        console.log('errorCode', errorCode);
        console.log('errorMessage', errorMessage);
      });
  }
}

接下来的 Imports 。您必须使用“ Compat”中的所有软件包,因为Firebase的新版本仍在开发中。因此,如果您使用此命令并且不起作用,请尝试使用所解释的兼容性版本。

It's working now.
First, I have to install "firebase" with npm i firebase
Second, I've removed everything in my app.module.ts I'm now using AngularFireModule and AngularFireAuthModule from "compat". See the code and pay attention to the imports

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';
import { AngularFireModule } from '@angular/fire/compat';
import { AngularFireAuthModule } from '@angular/fire/compat/auth';
import { environment } from '../environments/environment';

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFireAuthModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

Next, in app.component.ts also using AngularFireAuth from "@angular/fire/compat/auth"

import { Component } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/compat/auth';
import * as auth from 'firebase/auth';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.sass'],
})
export class AppComponent {
  title = 'angular-fire';

  constructor(private afAuth: AngularFireAuth) {}

  signIn() {
    return this.afAuth
      .signInWithPopup(new auth.GoogleAuthProvider())
      .then((result) => {
        const user = result.user;
        console.log(user);
      })
      .catch((error) => {
        const errorCode = error.code;
        const errorMessage = error.message;

        console.log('errorCode', errorCode);
        console.log('errorMessage', errorMessage);
      });
  }
}

Using ng add @angular/fire may drop you in troubles. You must use all packages from "compat" because the new version of firebase is still under development. So if you use this command and it's not working, try using the compatibility version as explained.

你的笑 2025-02-20 21:25:55

您可以尝试在app.module.ts上导入AngularFiremodule。你尝试过吗?

You can try to import the AngularFireModule on app.module.ts. Have you tried it?

停滞 2025-02-20 21:25:55

要正确实现Angular项目中的Firebase/GA4,以便在每条路线上进行跟踪。使用纱线或NPM安装角/火。

npm i @angular/fire

然后将初始化添加到app.module.ts并定义提供商。

import { NgModule } from '@angular/core';
import { AngularFireModule } from '@angular/fire/compat';
import {
  AngularFireAnalyticsModule,
  ScreenTrackingService,
  UserTrackingService,
} from '@angular/fire/compat/analytics';

import { environment } from '@env/environment';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [
    AppRoutingModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFireAnalyticsModule,
  ],
  providers: [ScreenTrackingService, UserTrackingService],
  bootstrap: [AppComponent],
})
export class AppModule {}

请记住将firebase设置添加到项目配置(环境变量)。

   firebase: {
    apiKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXX',
    authDomain: 'XXXXXX-analytics.firebaseapp.com',
    projectId: 'XXXXX-analytics',
    storageBucket: 'XXXXXX-analytics.appspot.com',
    messagingSenderId: 'CXXXXXX',
    appId: 'XXXXXX',
    measurementId: 'G-XXXXXXXX',
  },

ScreenTrackingService UserTrackingService 提供商确保将默认事件发送到每个路线上的firebase。

To correctly implement Firebase/GA4 in an Angular project so that tracking is done on each route. Install angular/fire using yarn or npm.

npm i @angular/fire

Then add initialization to import to app.module.ts and define providers.

import { NgModule } from '@angular/core';
import { AngularFireModule } from '@angular/fire/compat';
import {
  AngularFireAnalyticsModule,
  ScreenTrackingService,
  UserTrackingService,
} from '@angular/fire/compat/analytics';

import { environment } from '@env/environment';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [
    AppRoutingModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFireAnalyticsModule,
  ],
  providers: [ScreenTrackingService, UserTrackingService],
  bootstrap: [AppComponent],
})
export class AppModule {}

Remember to add Firebase settings to the project configuration (environment variables).

   firebase: {
    apiKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXX',
    authDomain: 'XXXXXX-analytics.firebaseapp.com',
    projectId: 'XXXXX-analytics',
    storageBucket: 'XXXXXX-analytics.appspot.com',
    messagingSenderId: 'CXXXXXX',
    appId: 'XXXXXX',
    measurementId: 'G-XXXXXXXX',
  },

The ScreenTrackingService and UserTrackingService providers ensure that default events are sent to Firebase on each route.

疑心病 2025-02-20 21:25:55

尝试在AppComponent的构造函数中初始化类型AUTH的变量(您必须从“@angular/fire/auth”中导入它)。

import { Auth } from '@angular/fire/auth' //import statement


export class AppComponent  {
      constructor(private auth: Auth){}   

      //I did this, it worked for me 
}

try initializing a variable of type Auth (you have to import it from '@angular/fire/auth') inside the constructor of AppComponent like this.

import { Auth } from '@angular/fire/auth' //import statement


export class AppComponent  {
      constructor(private auth: Auth){}   

      //I did this, it worked for me 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文