Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Meteor:如何在模板中获取iron router参数_Javascript_Meteor_Handlebars.js_Iron Router - Fatal编程技术网

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

Javascript Meteor:如何在模板中获取iron router参数,javascript,meteor,handlebars.js,iron-router,Javascript,Meteor,Handlebars.js,Iron Router,如何在模板中获取路由参数值 路由器 Router.map(function() { this.route('userpost', {path: '/mypost/:_id'}); this.route('usercomment', {path: '/mycomments/:_id'}); }); 我的当前位置是localhost:3000/mypost/12345。我想从route param分配路径参数 模板 <template name="mytemplate">

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

路由器

Router.map(function() {
  this.route('userpost', {path: '/mypost/:_id'});
  this.route('usercomment', {path: '/mycomments/:_id'});
});
我的当前位置是
localhost:3000/mypost/12345
。我想从route param分配路径参数

模板

<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>

请签出我编辑的问题。两个
a
标记都在同一个模板中为什么这是一个问题?只需确保
post
comment
是否在此模板上定义了帮助程序?我不知道为每个
a
标记创建帮助程序是否是一个好做法?我看不出有任何主要问题。请为具有计算路径的帮助程序添加代码好吗?
Template.mytemplate.helpers({
  context: function(){
    return {
      _id: Router.current().params._id
    };
  }
});