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
};
}
});