我想在我的Angular应用程序中使用了一些模块,但我正处于如何在我的角度应用程序中工作的十字路口,因为我需要在工厂文件中“require()”。
这是我感兴趣的节点模块:https://github.com/TimNZ/node-xero
在这个当前的项目中,我在Yeoman中使用Gulp Angular生成我的样板,如果我需要修改任何gulp scrips,我很难弄清楚我应该如何工作。
我在想我可以“浏览”将使用require()的单个文件,但这是错误的方法吗?我应该浏览所有项目文件吗?这是标准做法吗?
任何建议都表示赞赏,目前让我处于停滞状态。
我想要使用的与Xero相关的所有模块似乎都是节点模块。
答案 0 :(得分:2)
The simplest starting point would be to use Browserify to build a standalone bundle that uses a global of your choice.
To do this, you could create a JS file that requires the node module(s) you want to use. You could create a file named bundle-index.js
with this content:
exports.xero = require('node-xero');
You could then run this command to build a standalone module:
browserify --standalone the_global bundle-index.js > bundle.js
Where the_global
is a name you find appropriate for the global object that will contain the exports. With the bundle.js
file included in a script
element, you would be able use it like this:
var privateApp = new window.the_global.xero.PrivateApplication({ ... });
Doing things this way would involve the least amount of disruption to your current project. And if you are only needing to use Browserify to require third party libraries that don't change frequently, you could start with a simple manual process for building the standalone bundle.
Note that you can export other required modules by adding additional exports to bundle.js
:
exports.xero = require('node-xero');
exports.someOtherModule = require('some-other-module');
答案 1 :(得分:0)
(function(){
var xero = require('node-xero');
angular
.module('app')
.factory('myFactory', myFactory);
function myFactory(){
var helper = {
myMethod: myMethod,
};
return helper;
function myMethod(){
xero.doStuff();
}
}
})();