在不使用AngularFire的情况下在Angular 6应用程序中包含Firebase 5

时间:2018-07-16 16:51:27

标签: angular firebase

当我尝试将firebase包含到Angular应用程序中时收到错误消息(我不希望使用AngularFire2)

// app.component.ts

import { Component, OnInit } from '@angular/core';
import firebase from'firebase/dist/index.esm.js';
import 'firebase/auth';
import 'firebase/firestore';
import 'firebase/database';
import 'firebase/storage';

import { Router, NavigationStart, NavigationEnd, Event as NavigationEvent } from '@angular/router';
import { UserService } from './shared/user.service';

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

  constructor() {

  }

  ngOnInit() {

    const config = {
      apiKey: "AIzaSya8liEExAoc3jmGhokAk",
      authDomain: "firebaseapp.com",
      databaseURL: "https://hellodate.firebaseio.com",
      projectId: "hellodate-a0be3",
      storageBucket: "hellodate-a0be3.appspot.com",
      messagingSenderId: "6120921"
    };
    firebase.initializeApp(config);

  }
}

在浏览器中,我收到以下错误:

FirebaseError {code: "app/no-app", message: "Firebase: No Firebase App '[DEFAULT]' has been cre…- call Firebase App.initializeApp() (app/no-app).", name: "[DEFAULT]", stack: "[DEFAULT]: Firebase: No Firebase App '[DEFAULT]' h….module.ts (http://localhost:4200/main.js:213:77)"}
code
:
"app/no-app"
message
:
"Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app)."
name
:
"[DEFAULT]"
stack
:
"[DEFAULT]: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).↵    at error (http://localhost:4200/vendor.js:59708:21)↵    at app (http://localhost:4200/vendor.js:59591:13)↵    at Object.serviceNamespace [as firestore] (http://localhost:4200/vendor.js:59649:47)↵    at Object../src/app/shared/myfire.service.ts (http://localhost:4200/main.js:935:60)↵    at __webpack_require__ (http://localhost:4200/runtime.js:77:30)↵    at 
:
Error

如何在Angular应用程序中不使用angularFire的情况下包含原始Firebase库?

1 个答案:

答案 0 :(得分:0)

您运行过npm install firebase --save吗? 如果没有,那就这样做。

或者,您是否将自动导入语句用于Firebase?

如果是,请尝试更改导入:

import firebase from'firebase/dist/index.esm.js';

import firebase from "firebase";