问题描述
我正在使用SpineJS(它会导出commonjs模块),并且需要在全球范围内使用,因为我在任何地方都可以使用它,但是似乎我必须对每个使用Spine的文件执行Spine = require('spine')
才能正常工作.
I'm using SpineJS (which exports a commonjs module) and it needs to be available globally because I use it everywhere, but It seems like I have to do Spine = require('spine')
on every file that uses Spine for things to work.
有什么方法可以一次定义Spine
使其在全球范围内可用?
Is there any way to define Spine
once to make it globally available?
PS:我以Spine为例,但我总体上想知道如何与其他任何库一起使用.
PS: I'm using Spine as an example, but I'm in general wondering about how to do this with any other library.
推荐答案
在每个文件中写入Spine = require('spine')
是正确的方法.
Writing Spine = require('spine')
in each file is the right way to do.
但是,使用global
或window
对象有多种可能(browserify将global
对象设置为window
,这是全局名称空间):
Yet, there are several possibilities by using the global
or window
object (browserify sets the global
object to window
, which is the global namespace):
- 在spine.js中:
global.Spine = module.exports
- 在browserify捆绑的任何其他.js文件中:
global.Spine = require('spine')
- 在spine.js文件之后的.html文件引用的脚本标签或.js文件中:
window.Spine = require('spine')
- in spine.js:
global.Spine = module.exports
- in any other .js file bundled by browserify:
global.Spine = require('spine')
- in a script tag or an .js file referenced by the .html file, after the spine.js file:
window.Spine = require('spine')
这篇关于为Browserify定义全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!