首页 文章

错误:未创建Firebase应用程序'[DEFAULT]'

提问于
浏览
3

我正在使用Ionic2,当我转到localhost:8100(执行 ionic serve 后)后,我收到了您在下图中看到的错误 .

enter image description here

app.component.ts 看起来像这样:

import firebase from 'firebase';
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from 'ionic-native';
import { HomePage } from '../pages/home/home';

@Component({
  template: `<ion-nav [root]="rootPage"></ion-nav>`
})
export class MyApp {
  rootPage = HomePage;

  constructor(platform: Platform) {
    platform.ready().then(() => {


      var config = {
        apiKey: ".....",
        authDomain: "......",
        databaseURL: ".....",
        storageBucket: ".....",
        messagingSenderId: "......"
      };

      firebase.initializeApp(config);
      StatusBar.styleDefault();
    });
  }
}

app.module.ts

import { NgModule } from '@angular/core';
import { IonicApp, IonicModule } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';

@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage 
  ],
  providers: []
})
export class AppModule {}

My system information:

Cordova CLI:6.3.1

Ionic Framework版本:2.0.0-rc.1

Ionic CLI版本:2.1.1

Ionic App Lib版本:2.1.1

Ionic App Scripts版本:0.0.36

操作系统:分销商ID:Ubuntu描述:Ubuntu 16.04.1 LTS

节点版本:v4.2.6

1 回答

  • 9

    我有同样的问题,也许我有一个解决方案(适合我) .

    我已经从我的app.components.ts中完全删除了firebase初始化,并在app.module.ts中将其添加到NGModule之前,例如:

    ...
        import * as firebase from 'firebase';
    
        export const firebaseConfig = {
          apiKey: "",
          authDomain: ".firebaseapp.com",
          databaseURL: "https://.firebaseio.com",
          storageBucket: ".appspot.com",
          messagingSenderId: ""
        };
    
        firebase.initializeApp(firebaseConfig); //<-- where the magic happens
    
        @NgModule({
        ...
    

    现在我可以在我的服务中使用它(不要忘记将你的服务包含在app.module.ts的提供者中:[yourService]')

    import firebase from 'firebase';
    
    @Injectable()
    export class yourService {
    //Here you can use firebase.database(); or firebase.auth(); as you wish and it should work. 
    }
    

    希望这对你有用!

相关问题