Is there a way to access Iron Router parameter from template in Meteor

≯℡__Kan透↙ 提交于 2019-12-24 14:24:26

问题


I have a route that has a parameter in it and I need to access it from many different templates. Below is one example of the route, there are several routes that are very similar just after the _occasionnId parameter it changes:

For example:

Route 1: /occasions/:_occasionId/cards/

Router 2: /occasions/:_occasionId/tables/

Here is my full code for each route, the only thing that really changes is the route path and the template.

Router.route('/occasions/:_occasionId/cards/', {
template: 'cards',
data: function(){
//var currentoccasion = this.params._occasionId;
//console.log(currentoccasion);
},subscriptions : function(){
Meteor.subscribe('cards');
Meteor.subscribe('tables');
}
});

I need to get the _occasionId parameter into a template that has navigation which goes in on all of these pages. My goal is that from Route 1, you can go to Router 2. But I can't figure out how to add the correct URL in the template.

My template is:

<template name="occasionnav">
<nav>
  <div class="nav-wrapper">
    <ul class="right hide-on-med-and-down">
      <li><a href="/occasions/:_occasionId/cards/">cards</a></li>
      <li><a href="/occasions/:_occasionnId/tables/">tables</a></li>
    </ul>
  </div>
</nav>
</template>

In the 'occasionnav' template by ":_occasionId" I need that to be the same parameter as the page currently being viewed be stuck into here.

If anyone has any insight or advice on the best way to approach this I would really appreciate it.


回答1:


I recommend to use {{pathFor}} if you want to render an internal route in your Meteor application.

You just need to set the proper context and name your routes, for example:

<template name="occasionnav">
   <nav>
      <div class="nav-wrapper">
         <ul class="right hide-on-med-and-down">
            {{#with occasion}}
              <li><a href="{{pathFor route='occasions.cards'}}">cards</a></li>
              <li><a href="{{pathFor route='occasions.tables'}}">tables</a></li>
            {{/with}}
         </ul>
      </div>
   </nav>
</template>

Router.route('/occasions/:_id/cards/', {
    template: 'cards',
    name: 'occasions.cards',
    data: function() {
        return Cards.findOne({_id: this.params._id});
    },
    subscriptions: function() {
        return Meteor.subscribe('cards', this.params._id);
    }
});
Router.route('/occasions/:_id/tables/', {
    template: 'tables',
    name: 'occasions.tables',
    data: function() {
        return Tables.findOne({_id: this.params._id});
    },
    subscriptions: function() {
        return Meteor.subscribe('tables', this.params._id);
    }
});

However, you can also get the router parameters in your template via Router.current().params.




回答2:


You can pass the _occasionId as a template helper and render it in jade like:

<li><a href="/occasions/{{_occasionId}}/cards/">cards</a></li>



回答3:


You should try :

Router.route('/occasions/:_occasionId/cards/', function() {
  this.layout("LayoutName");
  this.render("cards", { 
     data: {
        currentoccasion = this.params._occasionId;
     }
  });
});

When you use Router.map, it's not exactly the same syntax:

Router.map(function() {
 this.route('<template name>', {
   path: 'path/:_currentoccasion',
   data: function () {
     return {
       currentoccasion: this.params._currentoccasion
     }
   }
});

And you can access just like that in your template :

Like an helper
{{ currentoccasion }}
Or on the onRendered and onCreated functions
Template.<template name>.onRendered({
  this.data.currentoccasion


来源:https://stackoverflow.com/questions/31871011/is-there-a-way-to-access-iron-router-parameter-from-template-in-meteor

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