如何在玉石模板中使用下划线

我想在玉石模板中使用下划线function,就像这样

p= _.keys(user) 

不适合客户端JavaScript,用于重复。

通过我在app.js中确实需要'下划线',没有相处得很好。 当然,它在app.js中正常工作。

 ReferenceError: xxxxxxx _ is not defined 

这是模板错误消息。 任何想法?

谢谢

如果您使用的是Express.js (大概是因为您使用的是Jade),您可以添加下划线作为视图助手 。

 app.helpers({ _: require("underscore") }); 

更新使用Express 3+,上述将不再工作,而是使用app.locals

 app.locals._ = require("underscore"); 

在Express 3.x帮助者被删除。 而是使用中间件和res.locals

 app.use(function(req, res, next){ res.locals._ = require('underscore'); next(); });