Meteor:如何在模板中获取 iron-router 参数

Meteor : How to get iron-router parameter in template

如何获取模板中的路由参数值?

路由器

Router.map(function() {
  this.route('userpost', {path: '/mypost/:_id'});
  this.route('usercomment', {path: '/mycomments/:_id'});
});

我现在的位置是localhost:3000/mypost/12345。我想从路由参数

分配一个路径参数

模板

<template name="mytemplate">
    <a class="tab-item" href="{{pathFor 'userpost' _id=???}}">Post</a>
    <a class="tab-item" href="{{pathFor 'usercomment' _id=???}}">Comment</a>
</template>

{{pathFor}} 正在使用当前数据上下文将 URL 参数替换为实际值,因此您需要将调用包含在 {{#with}} 块助手中。

<template name="mytemplate">
  {{#with context}}
    <a class="tab-item" href="{{pathFor "userpost"}}">Post</a>
    <a class="tab-item" href="{{pathFor "usercomment"}}">Comment</a>
  {{/with}}
</template>

context 是一个返回对象的助手,该对象具有 _id,而这个 属性 将用于填充计算路径。

Template.mytemplate.helpers({
  context: function(){
    return {
      _id: Router.current().params._id
    };
  }
});