通过browserify需要Angularfire

时间:2016-07-19 02:56:12

标签: javascript angularjs browserify angularfire npm-install

我已经通过NPM安装了Angularfire,但是当我尝试在我的Angular应用程序中使用它时,它说

  

firebase.initializeApp不是函数

这就是我现在尝试使用它的方式

var angular = require('angular');
var firebase = require('angularfire/index.js');

firebase.initializeApp({
    apiKey: "key",
    authDomain: "app.firebase.com",
    databaseURL: "app.firebase.com",
    storageBucket: "app.appspot.com",
});

var structurator = angular.module('structurator', ['firebase']);

那个index.js是因为我注意到Angularfire在index.js中使用这个代码导出自己

// Make sure dependencies are loaded on the window
require('angular');
require('firebase');

// Load the Angular module which uses window.angular and window.Firebase
require('./dist/angularfire');

// Export the module name from the Angular module
module.exports = 'firebase';

我还尝试在没有Firebase的情况下导入Angularfire,导入Angularfire和Firebase并引用它们......等等。

1 个答案:

答案 0 :(得分:0)

首先,由于index.js被指定为angularfire / package.json下的主文件,因此只能require('angularfire')

您还需要将导入分配给变量:

var firebase = require('firebase');
var angular = require('angular');
var angularfire = require('angularfire');

然后你的其余代码应该可以工作:

var firebase = require('firebase');
var angular = require('angular');
var angularfire = require('angularfire');

firebase.initializeApp({
    apiKey: "key",
    authDomain: "app.firebase.com",
    databaseURL: "app.firebase.com",
    storageBucket: "app.appspot.com",
});

var structurator = angular.module('structurator', ['firebase']);