在没有angularfire的情况下以角度6初始化firebase [英] initialize firebase in angular 6 without angularfire
问题描述
我想要一个使用 AngularFire 初始化 firebase.然而,在整个开发过程中,我并没有使用 AngularFire 的太多功能.我总是在每个服务中导入 firebase/app
并使用相应的函数,如 firebase.auth()
或 firebase.database()
.
I want had a project that initialize firebase by using AngularFire. However, during the entire development I didn't use much function from AngularFire. I always import the firebase/app
at every services and use the respective function like firebase.auth()
or firebase.database()
.
有了这个经验,我想在没有 AngularFire 的情况下初始化 Firebase,因为我没有使用 AngularFire 方法.
With that experience, I would like to initialize the firebase without AngularFire since I am not using the AngularFire methods.
现在我的问题是我找不到任何资源来教我如何初始化 firebase,当然我在 app.module.ts
导入 firebase 时看到错误.
Now come with my problem is I can't find any source to teach me how to initialize the firebase, and of course I am seeing error during importing the firebase at app.module.ts
.
下面是我的代码:
使用以下命令安装 firebase:npm i firebase
Install the firebase by using: npm i firebase
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import * as firebase from 'firebase';
import { environment } from '../environments/environment'; //API key is imported
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
firebase.initializeApp(environment.firebase) //here shows the error
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
在firebase.initializeApp(environment.firebase)
这一行,显示错误:
Type 'App' is not assignable to type 'any[] | Type<any> |
ModuleWithProviders<any>'.
Type 'App' is not assignable to type 'ModuleWithProviders<any>'.
Property 'ngModule' is missing in type 'App'.
提前致谢.
推荐答案
我会说你有这个错误,因为 firebase.initializeApp(environment.firebase)
不是 Angular Module.
I would say you are having this error because firebase.initializeApp(environment.firebase)
is not Angular Module.
我建议您在其中一个服务中进行此初始化或为此效果创建一个服务.
I would suggest that you do this initialisation in one of your services or create a service for that effect.
@Injectable({
providedIn: 'root'
})
export class FirebaseService {
constructor() {
firebase.initializeApp(environment.firebase)
}
}
这篇关于在没有angularfire的情况下以角度6初始化firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!