【问题标题】:Expecting 'ID', got 'NUMBER'期待“ID”,得到“NUMBER”
【发布时间】:2017-01-07 17:15:57
【问题描述】:

我在json中有以下数据

"ICON":{
    "144":"https://example.com/bubble-academy.jpg",
    "228":"https://example.com/bubble-academy.jpg",
    "72":"https://example.com/bubble-academy.jpg",
    "152":"https://example.com/bubble-academy.jpg",
    "130":"https://example.com/bubble-academy.jpg",
    "120":"https://example.com/bubble-academy.jpg",
    "32":"https://example.com/bubble-academy.jpg"
}

在车把中,我正在尝试访问属性 32,如下所示。

<img src="{{ ICON.32 }}">

我得到以下错误

Module build failed: Error: Parse error on line 5:
..."{{ mediaFiles.ICON.32  }}">        <sp
-----------------------^
Expecting 'ID', got 'NUMBER'

我该如何解决这个问题?

【问题讨论】:

  • 不知道手把,我猜{{ ICON[32] }}edit: 啊,应该是ICON.[32]。文档:handlebarsjs.com/expressions.html
  • 已经试过了,没用。
  • ICON.[32],它成功了。
  • Uriel 是对的,不幸的是他们删除了他们的答案:-/
  • 大家在这里学到了什么?始终阅读documentation

标签: javascript handlebars.js


【解决方案1】:

这是因为您使用数字作为属性名称,请尝试改用:

&lt;img src="{{ ICON.[32] }}"&gt;

【讨论】:

  • 上面写着Expecting 'ID', got 'INVALID'
  • ICON.[32] 应该是ICON[32]
【解决方案2】:

这里还有一个让人迷惑的有趣案例: (这就是静态生成的 wordpress 表单的外观)

Input
{
    "body" : {
        'input_2.2': 'Mrs.',
        'input_2.3': 'Cynthia',
        'input_2.6': 'Winterbottom',   
        'input_6': 'Professor',
  }
}

此模板导致空白

<p>Form submission</p>
  <p>{{body.input_2.[2]}} {{body.input_2.[3]}} {{body.input_2.[6]}}</p>
  <p>{{body.input_6}}</p>

Result:
<p></p>
<p>Professor</p>

显然,由于所有数字条目都不存在...您不能简单地将它们括起来...您必须一次对整个子路径进行分组

Proper template:
<p>Form submission</p>
  <p>{{body.[input_2.2]}} {{body.[input_2.3]}} {{body.[input_2.6]}}</p>
  <p>{{body.input_6}}</p>

这是通过反复试验发现的...找不到解释此问题的适当文档

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-17
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多