0

我正在尝试使用 Handlebars.js 访问 API 中的元数据对象,但在控制台中我不断收到错误消息“缺少帮助程序:元”。我不确定是什么原因造成的,感谢您的帮助。

 <body>
    <div class="container" id="content"></div>
    <template id="template">
      <div class="info">
        {{Meta Data}}
      </div>
    </template>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.1.2/handlebars.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
    <script src="index.js"></script>
  </body>
</html>

我的 JS 文件:

const content = document.getElementById('content');
const template = document.getElementById('template').innerHTML;
const info = document.querySelector('.info');

function render(context) {
  let compiled = Handlebars.compile(template);
  template.innerHTML = compiled(context);
}

$.ajax({
  url:
    'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&apikey=demo',
  method: 'GET'
})
  .done(function(stonk) {
    console.log(stonk);

    content.innerHTML = render(template, { info: stonk });
  })
  .fail(function(error) {
    console.error('Something went wrong', error);
  });
4

1 回答 1

0

我对 Handebars 不是很熟悉,但您可能会将问题归结为关键是带有空格的“元数据”这一事实。在您正在调用的车把模板中{{Meta Data}},它将 Meta 视为帮助程序的名称,而不是 Object 键。

于 2020-09-01T06:09:06.850 回答