【问题标题】:Precompiling handlebars template with custom helpers使用自定义助手预编译车把模板
【发布时间】:2023-04-03 20:02:02
【问题描述】:

我有一个message.handlebars 模板,在浏览器中编译它时效果很好。

我用handlebars -message.handlebars -f -message.js成功预编译了它。

然后在我的 ASP.NET MVC 站点中,我将引用添加为:

<script src="~/Scripts/handlebars.runtime-v1.3.0.js"></script>
<script src="~/Scripts/handlebars-helpers.js"></script>
<script src="~/Scripts/templates/message.js"></script>

handlebars-helpers.js 包含消息模板使用的一些自定义助手。

我从这个人的comment 那里采用了这种方法,但它不起作用。执行var html = Handlebars.templates.message(data); 时出现错误。在浏览器中编译时,这再次完美运行,因此这不是上下文问题。

我得到的错误是:

这是预编译和使用自定义助手的正确方法吗?或者有没有办法将这些助手包含在预编译的模板中?

【问题讨论】:

    标签: javascript handlebars.js helpers precompiled-templates


    【解决方案1】:

    这是服务器上的车把版本。 NPM 默认安装最新版本,并且版本 2 alpha 导致了该问题。在 Github (https://github.com/wycats/handlebars.js/issues/734) 上找到了这个修复:

    npm uninstall handlebars -g
    npm install handlebars@1.3 -g
    

    一切正常!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多