我在这些问题上苦苦挣扎了一段时间,然后才意识到还有更好的选择。我建议将 JavaScript 函数传递给 Pug 渲染函数,而不是将它们构建到模板中。
我之前做的是这个 JavaScript
const render = pug.compileFile(path.join(__dirname, '../templates/sandbox.pug'));
const html = render({});
还有这个 Pug 模板
- var testFunc = function(){
- return "Test func";
- }
div #{testFunc()} worked!
实现同样事情的更好方法是使用这个 JavaScript
const render = pug.compileFile(path.join(__dirname, '../templates/sandbox.pug'));
const html = render({
testFunc: function(){
return "Test func";
}
});
还有这个 Pug 模板
div #{testFunc()} worked!
这允许您设置断点,使用 Typescript 和所有其他很酷的东西,并避免所有与解析 JavaScript 相关的 Pug 错误。