本文介绍了Node.js 全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里问:Node.js 需要继承吗?
I asked here:Does Node.js require inheritance?
有人告诉我,我可以通过省略变量来将变量设置为全局范围.
And I was told that I can set variables to the global scope by leaving out the variable.
这对我不起作用.
也就是说,以下内容不会使 _
在所需文件上可用.
That is, the following does not make the _
available on required files.
_ = require('underscore');
我可以使用 Express.js 的 app.set
进行设置,但可以在其他地方使用.
I can set with Express.js's app.set
and have it available elsewhere though.
这是应该的工作方式吗?
Is that how this is supposed to work?
推荐答案
您可以使用 全局
像这样:
You can use global
like so:
global._ = require('underscore')
这篇关于Node.js 全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!