【发布时间】:2017-11-10 05:30:17
【问题描述】:
虽然我可以传递对象的数据,但我不知道如何传递/调用对象的函数。
route.js:
router.get('/', function(req, res, next) {
let test = {}; // passing an object called test
test.hello = function() { //the test object has a method I want to
console.log('hello'); //call on the browser
}
res.render('home.jade', { test:test });
.jade 页面上:
//- let test = !{test}; //renders as [object Object]
let test = !{JSON.stringify(test, null, 4)}; //renders empty obj
test.hello();
console.log('test', test);
控制台消息:
Uncaught TypeError: test.hello is not a function
渲染的源文件:
//- let test = [object Object];
let test = {};
test.hello();
console.log('test', test);
对 my.jade 文件有效的示例(我不想要的):
let test = {};
test.hello = #{test.hello};
test.hello();
这将安慰出“你好”。但是,我想有一种方法可以在没有这种解决方法的情况下传递和调用对象的函数。
感谢您的帮助。
【问题讨论】: