node.js需要继承吗? [英] node.js require inheritance?

查看:67
本文介绍了node.js需要继承吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的server.js中我包含了下划线库。

in my server.js i included the underscore library.

var _ = require('underscore')

我有这样的路线:

// require routes
require('./routes/document');

在文档路径中,我想使用下划线。但似乎_ var不是继承/在文档范围内。这是否意味着我必须在每个必需的路线上设置_ var?或者有更智能的方法吗?

In the document route, I want to use underscore. But it seems like the _ var is not inherited/in side the document scope. Does that mean I have to set the _ var on every single required route? Or is there a more intelligent way to do this?

谢谢。

推荐答案

是的,你应该在需要它的文件中设置_。

Yes, you should set the _ in the files that needs it to be available.

或者,你可以通过删除<$将它放在全局范围内c $ c> var part。

Alternatively, you can put it in the global scope by removing the var part.

_ = require('underscore');
require('./routes/document'); // _ will be visible in document as well

这篇关于node.js需要继承吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆