我正在尝试在 express-handlebar 中传递一个函数, 但它不工作。 我使用 app.js 作为服务器文件,使用 index.handlebars 作为 Handlebars 文件。
应用程序.js
const express=require('express');
const app=express();
const csc=require('countrycitystatejson');
const exphbs=require('express-handlebars');
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
// console.log(csc.getAll());
function hello(){
console.log('hello');
}
app.get('/', function (req, res) {
res.render('index',{
hello:hello
});
});
app.listen(3000);
index.handlebars
<button onclick="hello()">click</button>
最佳答案
最好为此创建一个助手。下面是一个例子并引用Github有关该主题的更多信息。
const express=require('express');
const app=express();
const csc=require('countrycitystatejson');
const exphbs=require('express-handlebars');
var hbs = exphbs.create({
helpers: {
hello: function () { console.log('hello'); }
}
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// console.log(csc.getAll());
app.get('/', function (req, res) {
res.render('index');
});
app.listen(3000);
index.handlebars
<button onclick="{{hello}}">click</button>
关于javascript - 如何将函数传递给 nodejs-express 中的 express-handlebars?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56399436/