Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
从 express 我将变量 locals 传递给模板引擎,如下所示:
res.render("Main.pug", locals );
我可以在模板引擎中使用该变量。但我也想在包含的脚本文件中使用它
脚本(src='/file/Index.js')
我试过以下,
script(src='/file/Index.js') | var myVar = "Hello";
但不工作。
现在它的工作,
需要高于任何其他脚本文件
脚本(类型=“文本/javascript”)。var myVar =“测试”;
在 Index.js 中:
alert(myVar)
如果您想使用从 express 传递到脚本文件的变量,请使用:
script(type="text/javascript"). var myVar ="#{JSON.stringify(data)}";