在Angular 13项目中添加firebase不起作用
我仍在尝试在一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
现在正在工作。
首先,我必须使用
npm I Firebase
安装“ firebase”其次,我现在使用了
angularFireModule
和angularFireAuthModule
从“ compat”中删除了我现在使用的所有内容。 请参阅代码并注意在App.component.ts中,
接下来的 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
andAngularFireAuthModule
from "compat". See the code and pay attention to the importsNext, in app.component.ts also using AngularFireAuth from "@angular/fire/compat/auth"
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.您可以尝试在app.module.ts上导入AngularFiremodule。你尝试过吗?
You can try to import the AngularFireModule on app.module.ts. Have you tried it?
要正确实现Angular项目中的Firebase/GA4,以便在每条路线上进行跟踪。使用纱线或NPM安装角/火。
然后将初始化添加到
app.module.ts
并定义提供商。请记住将firebase设置添加到项目配置(环境变量)。
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.
Then add initialization to import to
app.module.ts
and define providers.Remember to add Firebase settings to the project configuration (environment variables).
The ScreenTrackingService and UserTrackingService providers ensure that default events are sent to Firebase on each route.
尝试在AppComponent的构造函数中初始化类型AUTH的变量(您必须从“@angular/fire/auth”中导入它)。
try initializing a variable of type Auth (you have to import it from '@angular/fire/auth') inside the constructor of AppComponent like this.