【问题标题】:Handlebars does not appear to allow me to access an array inside my object passed to the templateHandlebars 似乎不允许我访问传递给模板的对象内的数组
【发布时间】:2020-04-21 18:59:30
【问题描述】:

这个问题只是在我将代码迁移到生产环境时才出现的,在我的开发环境中不会发生。

我有一个接收对象的车把模板,例如:

{node: {sections: [{name: 'abc'},{name: 'xyz'}]}}

模板如下所示:

{{#each node.sections}}
  {{#ifCond type '==' "image-left-text-right"}}
    {{> admin/section/image-left-text-right}} 
  {{/ifCond}}
  {{#ifCond type '==' "two-columns-image-and-text"}}
    {{> admin/section/two-columns-image-and-text}} 
  {{/ifCond}}
  {{#ifCond type '==' "four-columns"}}
  {{> admin/section/four-columns}} 
  {{/ifCond}}     
{{/each}} 

现在这一切在我的开发环境中看起来都很棒,并且模板正确呈现,在生产中没有任何显示。

在控制台记录我的整个对象后,我可以确认它在那里,写了一个小助手来帮助我控制台记录它:

<script>
  console.log('----1---')
  console.log({{{json node}}})
  console.log('----2---')
  console.log({{{json node.sections}}})
  console.log('----3---')
</script>

超级奇怪的是节点上的console.log返回:

但是第二个控制台日志什么也没输出。

好像它无法访问对象上的那个属性?

我在 dev 和 prod 中都运行版本 handlebars@4.7.6。在我需要设置的 Handlebars 中是否有一些我不知道的奇怪的私有/公共标志,以允许它直接访问数组?

【问题讨论】:

    标签: node.js handlebars.js


    【解决方案1】:

    所以,显然我错了,我在开发中运行 4.0.12,在生产中运行 4.7.6。 package.json 文件中的 ^4.0.12 导致它安装较新的版本。显然这个问题与 4.7.6 有关,目前我已将其更改为 4.0.12,并将最终调查 4.7.6 中的哪些更改导致了此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      相关资源
      最近更新 更多