下划线 _.模板无法识别变量

underscore _.template not recognizing variable

本文关键字:识别 变量 下划线      更新时间:2023-09-26

我阅读了最后一个线程,找不到任何语法错误。我正在尝试在下划线方法中使用 _.template 方法,但它无法识别我传入的对象。我正在使用咖啡脚本,也许这搞砸了?

template = _.template("<h1>Temperature: <%= temp %> </h1>", {temp: 'test'})
$('main').append(template)

当我执行此代码时,我收到错误:

 Uncaught ReferenceError: temp is not defined

我不明白这是怎么回事,我完全遵循下划线模板。为什么 _.template 无法识别我传递给它的变量?

试试这个

template = _.template("<h1>Temperature: <%= temp %> </h1>")({temp: 'test'})