我有这个模板脚本
<script id="some-template" type="text/x-handlebars-template">
{{#users}}
{username}
{email}
</script>
我想将其外包到名为“user_template.js”的文件中,该文件如下所示:
{{#users}}
{username}
{email}
并在主 index.html 中创建此链接:
<script id="some-template" type="text/x-handlebars-template" src="user_template.js"></script>
问题是 - 它不起作用 - 我该怎么做?
最佳答案
我会为此使用 RequireJS。很高兴与模块一起工作,还有一个名为 text 的插件可以很好地与模板一起工作。
如果听起来很有趣,这里有一些链接:
http://requirejs.org/docs/download.html#text -- 文本插件
RequireJS 只适用于你想使用模块的情况,否则 Alon 的答案会更好。
关于javascript - 使用 Handlebars.js 时如何将模板 js 外包到不同的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12035839/