我遇到问题,无法在 pug 文件的脚本标记中传递变量。
- var toto = JSON.stringify({"lol":"azd", "lol2":"gdc"});
script.
$(document).ready(function () {
var te = JSON.parse("#{toto}");
console.log(te.lol);
});
结果为 html:
$(document).ready(function () {
var te = "{"lol":"azd","lol2":"gdc"}";
console.log(te.lol);
});
控制台结果:
Uncaught SyntaxError: Invalid or unexpected token
感谢您的帮助!
帕诺留斯。
最佳答案
原因是toto
的值被转义了。使用 !
使用未转义的值:
- var toto = JSON.stringify({"lol":"azd", "lol2":"gdc"});
script.
$(document).ready(function () {
var te = JSON.parse("!{toto}");
console.log(te.lol);
});
关于javascript - PUG:无法将对象传递到脚本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60736342/