我正在尝试利用 moment.js
的优势来解析 sails 应用下的 jade 模板中的日期。
我读过,为了在 View 服务器端使用 moment.js
,我必须利用 locals
的概念。
因此根据documentation我正在将路线修改为:
var moment = require("moment");
...
'get /post/:post_id': {
controller: 'PostController',
action: 'readPostById',
locals: { moment: moment } // adding moment.js
},
但是当我尝试使用 in from jade 时
...
- var date = moment().format('YYYY');
span= date
...
..我收到错误消息说moment is not a function。
我做错了什么?
最佳答案
这是我自己想出来的。背后的原因是在路由中定义 locals
与在 res.render()
上为模板传递参数相同。在我的例子中,我使用模板传递参数/数据,这似乎会覆盖使用 locals
传递的内容。
因此,如果您想在模板中使用 moment
,请在渲染时传递它,而不是使用 locals
,如下所示:
var moment = require("moment");
...
showPage: function(req, res) {
res.render('page.jade', {moment: moment, other_data: "just to test"});
};
关于javascript - 风 sails : locals from jade template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35719974/