【问题标题】:Cannot read property 'helpers' of undefined无法读取未定义的属性“助手”
【发布时间】:2014-11-09 00:51:58
【问题描述】:

Meteor 已完成测试版,我很高兴能使用它。我将它安装在我的 Windows 机器上(来自http://win.meteor.com/)并创建了一个应用程序。代码如下:

html:

<!-- simple-todos.html -->
<head>
  <title>Todo List</title>
</head>

<body>
  <div class="container">
    <header>
      <h1>Todo List</h1>
    </header>

    <ul>
      {{#each tasks}}
        {{> task}}
      {{/each}}
    </ul>
  </div>
</body>

<template name="task">
  <li>{{text}}</li>
</template>

javascript:

// simple-todos.js
if (Meteor.isClient) {
  // This code only runs on the client
  Template.body.helpers({
    tasks: [
      { text: "This is task 1" },
      { text: "This is task 2" },
      { text: "This is task 3" }
    ]
  });
}

它与official meteor tutorial 中的代码完全相同。如果我运行该示例,则标题会很好地呈现。另一方面,列表根本没有出现。不知何故,助手不起作用。我收到以下 javascript 错误:

未捕获的类型错误:无法读取未定义的属性“助手”

在流星控制台中,没有打印错误或警告。我对流星感到非常兴奋,我很想在未来将它用于生产应用程序,所以请帮助我解决这个(可能非常简单)的问题。我希望这不仅仅是windows中的问题(流星还没有正式发布windows)。

【问题讨论】:

  • 我要做的第一件事是在尝试调用.helpers() 之前添加console.dir(Template);。问题是.bodyundefined,所以弄清楚Template 的实际样子会很有用。

标签: javascript meteor


【解决方案1】:

模板需要由&lt;template name="xxx"&gt; 定义。

&lt;body&gt; 元素上没有助手。

不过,您可以为此使用全局帮助器:

Template.registerHelper("tasks", function() {
    return [....]
});

另一种方法是使用&lt;template name="body"&gt;

<body>
    {{>body}}
</body>

<template name="body">
  <div class="container">
    <header>
      <h1>Todo List</h1>
    </header>

    <ul>
      {{#each tasks}}
        {{> task}}
      {{/each}}
    </ul>
  </div>
</template>

【讨论】:

  • 文档不同意,使用Template.body.helpers 应该可以正常工作。 docs.meteor.com/#/full/template_body
  • @PeppeL-G 是的,你是对的。我认为他们不赞成使用 UI->模板转换
猜你喜欢
  • 2018-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-10
  • 1970-01-01
  • 1970-01-01
  • 2019-01-25
  • 2018-12-04
相关资源
最近更新 更多