Meteor Template不会在每个语句中输出数据

Meteor Template does not output data in each statement

本文关键字:语句 输出 数据 Template Meteor      更新时间:2023-09-26

我有以下三个文件:

  • abc.js
  • abc.html
  • router.js

如果HTML被模板包围,则HTML中的每个语句都不输出数据。删除模板是在输出数据。

//file name : abc.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" }
    ]
  });
}
//file name router.js
Router.map(function () {
  this.route('mylist',{
    path:'/list',
  });
});
//file name: abc.html
<template name="mylist">
<body>
  <div class="container">
    <header>
      <h1>Todo List</h1>
    </header>
    <ul>
      {{#each tasks}}
        {{> task}}
      {{/each}}
    </ul>
  </div>
</body>
</template>
<template name="task">
  <li>{{text}}</li>
</template>

为什么模板不使用#each语句输出数据?

您正在使用Template.body.helpers而不是mylist助手。
试着这样修改你的代码:

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

您还应该从mylist模板中删除<body>标签,并将模板mylist包含在html body:

<body>
{{>mylist}}
</body>
<template name="mylist">
  <div class="container">
    <header>
      <h1>Todo List</h1>
    </header>
    <ul>
      {{#each tasks}}
        {{> task}}
      {{/each}}
    </ul>
  </div>
</template>
<template name="task">
  <li>{{text}}</li>
</template>