0

我试图通过单击显示“查看更多”的链接将我的#each 项目循环中的单个记录打开到它自己的视图中,这将带我进入单个文章。我设置了我的 Flow 路由器及其工作,但我看不到应该进入的数据。

单篇文章的模板助手看起来像这样

Template.collectionSingle.helpers({
    articles: function () {
      var id = FlowRouter.getParam('_id')
      return theCollection.findOne({_id: id});
    }
  });

}

我的路线看起来像这样

FlowRouter.route('/collection/:_id', {
    name: 'collection',
    action() {
        BlazeLayout.render("AppLayout", {main: "collectionSingle"});
    }
});

模板“collectionSingle”看起来像这样

<template name="collectionSingle">
<h1>{{title}}</h1>
<h1>This is a test</h1>
  <img src="{{thumbnail}}" alt="" />
</template>

当我导航到http://localhost:3000/collection/thePublication-0时 ,我看到的只是测试消息“这是一个测试”,但我没有看到 {{title}} 也没有缩略图。

此外,当我改变时:

return theCollection.findOne({_id: id});

到我的另一个收藏:

return OtherCollection.findOne({_id: id});

http://localhost:3000/collection/thePublication-0 

保持不变。

我怎样才能成功地为我的每篇文章创建一个单独的文章页面,并将它们与流路由器正确链接?

4

1 回答 1

1

您需要实际使用返回数据上下文的模板助手:

<template name="collectionSingle">
<h1>{{title}}</h1>
<h1>This is a test</h1>
{{#with articles}}
  <img src="{{thumbnail}}" alt="" />
{{/with}}
</template>

由于您的articles助手返回您使用的单个文档{{#with articles}}。如果它返回一个游标或数组,您将使用{{#each articles}}. 通常的惯例是前者使用单数形式,后者使用复数形式。

于 2016-06-10T01:05:44.523 回答