Angular Typescript中的Firebase InitializeApp

时间:2016-11-18 17:47:09

标签: angular firebase systemjs

我试图初始化我的angular(typescript)应用程序以使用firebase。我有一个FirebaseService类,它有一个初始化应用程序的方法,即

import {Injectable} from "@angular/core";
import * as firebase from 'firebase';

const firebaseConfig = {
    apiKey: // my api key,
    authDomain: // my auth domain,
    databaseURL: // my database url,
    storageBucket: // my storage bucket,
};

@Injectable()
export class FirebaseService {

    start(): void {
        console.log("Starting firebase");
        firebase.initializeApp(firebaseConfig);
    };
}

我在应用程序启动时调用FirebaseService.Start,但我目前在浏览器控制台中收到以下错误

Starting firebase
core.umd.js:2837 EXCEPTION: Error in :0:0 caused by: firebase.initializeApp is not a functionErrorHandler.handleError @ core.umd.js:2837
core.umd.js:2839 ORIGINAL EXCEPTION: firebase.initializeApp is not a functionErrorHandler.handleError @ core.umd.js:2839
core.umd.js:2842 ORIGINAL STACKTRACE:ErrorHandler.handleError @ core.umd.js:2842
core.umd.js:2843 TypeError: firebase.initializeApp is not a function
    at FirebaseService.start (http://localhost:3000/app/services/firebase.service.js:24:18)
    at new AppComponent (http://localhost:3000/app/app.component.js:16:25)
    at new Wrapper_AppComponent (/AppModule/AppComponent/wrapper.ngfactory.js:7:18)
    at CompiledTemplate.proxyViewClass.View_AppComponent_Host0.createInternal (/AppModule/AppComponent/host.ngfactory.js:20:28)
    at CompiledTemplate.proxyViewClass.AppView.createHostView (http://localhost:3000/lib/@angular/core/bundles/core.umd.js:9147:25)
    at CompiledTemplate.proxyViewClass.DebugAppView.createHostView (http://localhost:3000/lib/@angular/core/bundles/core.umd.js:9407:56)
    at ComponentFactory.create (http://localhost:3000/lib/@angular/core/bundles/core.umd.js:5481:29)
    at ApplicationRef_.bootstrap (http://localhost:3000/lib/@angular/core/bundles/core.umd.js:6550:44)
    at eval (http://localhost:3000/lib/@angular/core/bundles/core.umd.js:6459:93)
    at Array.forEach (native)

我的SystemJS配置设置如下

(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            "lib:": 'lib/'
        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder

            // angular bundles
            '@angular/core': 'lib:@angular/core/bundles/core.umd.js',
            '@angular/common': 'lib:@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'lib:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'lib:@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'lib:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'lib:@angular/http/bundles/http.umd.js',
            '@angular/router': 'lib:@angular/router/bundles/router.umd.js',
            '@angular/forms': 'lib:@angular/forms/bundles/forms.umd.js',
            'rxjs': 'lib:rxjs',
            'firebase': 'lib:firebase'
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                main: './main.js',
                defaultExtension: 'js'
            },
            rxjs: {
                defaultExtension: 'js'
            },
            firebase: {
                main: 'firebase.js',
                defaultExtension: 'js'
            }
        }
    });
})(this);

因此它应该可以从我的firebase.js文件夹中加载lib。查看firebase.js肯定会出现一个名为initializeApp的函数,即

initializeApp:function(a,c){void 0===c?c="[DEFAULT]":"string"===typeof c&&""!....

所以我无法解决我出错的地方。有什么想法吗?

我使用angular v2.2.0和firebase v3.6.1

1 个答案:

答案 0 :(得分:1)

我想知道同样的事情:

  

我可以直接使用firebase而不使用angularfire2吗?

但无论如何,您可以将angularfire2库用于Angular项目。

https://github.com/angular/angularfire2

对于System.jsthis doc中有更多信息。

修改

也可以使用不firebase的{​​{1}},例如:

angularfire2