Meteor 1.0 - 将参数传递给路径使用iron:router

时间:2015-02-11 18:10:01

标签: meteor iron-router

我已经看到了一些关于如何使用iron:router作为第三个参数传递参数的答案,例如:href="{{pathFor 'article' _id=this._id }}"

我想要做的是传递一个参数作为第二个变量(上面的例子中'article')。

我有一个posts的集合,其中包含titlebodytype。我正在处理的项目中有三种类型的文章,每种类型都需要路由到不同的模板,因为格式有很大不同。

我希望能够根据类型路由到特定网址。捕获我的意图但不起作用的一个例子是:

<a href="{{pathFor '{{type}}'}}">link</a>

其中一个“类型”是“查询”,路线如下:

 @route "inquiryPost",
    path: "inquiry/:_id"
    data: ->
      Posts.findOne @params._id
    waitOn: ->
      [
        Meteor.subscribe "posts"
      ]

我尝试使用帮助器将值传递给pathFor,但这也不起作用:

path: ->
    type = @type
    "pathFor '#{type}'"

......在前端有这个:

<a href="{{path}}">link</a>

我可以想到一些解决方法,但在我看来应该有一种更简单的方法来传递参数......

那么,是否可以将参数传递给pathFor的第二个值?

1 个答案:

答案 0 :(得分:4)

尝试

   <template name="example">
    <a href="{{pathFor type}}">link</a>
   </template>

其中type是帮助者

Template.example.examples({
 type:function(){
  return type;
 }
})

你也可以试试这个例子

<a href="/myRoute/{{myHelper}}/{{this._id}}">link with many parameters</a>

在这里您可以访问例如路线

例如

/myRoute/type/rewr8671qew

相关问题