1
オブジェクトのデータを渡すことはできますが、オブジェクトの関数を渡したり呼び出したりする方法はわかりません。Express/Jade/Pug:javascriptオブジェクトの関数を呼び出す
route.js:.jadeページで
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 });
:
//- 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();
これは 'ハロー' 出て慰めるます。しかし、私はこの回避策なしでオブジェクトの関数を渡して呼び出す方法があると思います。
ありがとうございました。