简体   繁体   English

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

[英]Meteor : How to get iron-router parameter in template

How to get get route param values in template? 如何在模板中获取路由参数值?

Router 路由器

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

my current location is localhost:3000/mypost/12345 . 我当前的位置是localhost:3000/mypost/12345 I want to assign a path parameter from a route param 我想从路由参数中分配路径参数

Template 模板

<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}} is using the current data context to replace URL parameters with actual values, so you need to enclose the call inside a {{#with}} block helper. {{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 is a helper returning an object that have an _id , and this property will be used to fill-in the computed path. context是一个帮助器,返回一个具有_id的对象,该属性将用于填充计算出的路径。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM