Meteor 1.0 - Passing a parameter into pathFor using iron:router

自古美人都是妖i 提交于 2020-02-23 07:36:08

问题


I've seen a few answers on how to pass a parameter using iron:router as the third parameter, for example: href="{{pathFor 'article' _id=this._id }}"

What I would like to do is pass a parameter as the second variable (where 'article' is in the example above).

I have a collection of posts that contain a title, a body, and a type. There are three types of articles in the project I'm working on, and each type needs to be routed to a different template because the formatting is significantly different.

What I would like to be able to do is route to a particular url based on the type. An example that captures my intent but doesn't work would be this:

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

One of the "types" is "inquiry", and the route looks like this:

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

I've tried using a helper to pass the value into the pathFor, but that didn't work either:

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

...with this on the front end:

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

I can think of a few work arounds, but it seems to me like there should be an easier way to pass in parameters...

So, is it possible to pass in parameters into the second value of a pathFor?


回答1:


Try with

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

Where type is the helper

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

Also you can try with this for example

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

Here you where accessing for example to the route

/myRoute/type/rewr8671qew for example



来源:https://stackoverflow.com/questions/28461357/meteor-1-0-passing-a-parameter-into-pathfor-using-ironrouter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!