我已经通过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并同时引用两者...等等。

最佳答案

首先,由于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']);

关于javascript - 通过browserify要求Angularfire,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38448857/

10-12 14:09