有没有办法从Meteor中的模板访问Iron Router参数 [英] Is there a way to access Iron Router parameter from template in Meteor

查看:88
本文介绍了有没有办法从Meteor中的模板访问Iron Router参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条包含参数的路由,我需要从许多不同的模板中访问它.以下是该路线的一个示例,在更改_occasionnId参数之后,有几条非常相似的路线:

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:

例如:

路线1:/occasions/:_ occasionId/cards/

Route 1: /occasions/:_occasionId/cards/

路由器2:/occasions/:_ occasionId/tables/

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

我需要将_occasionId参数放入具有导航功能的模板中,该模板可在所有这些页面中使用.我的目标是从路由1转到路由器2.但是我不知道如何在模板中添加正确的URL.

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.

我的模板是:

<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>

在"occasionnav"模板中,通过:_occasionId",我需要将其与当前正在查看的页面设置为相同的参数.

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.

推荐答案

如果要在Meteor应用程序中呈现内部路线,我建议使用{{pathFor}}.

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

但是,您也可以通过Router.current().params在模板中获取路由器参数.

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

这篇关于有没有办法从Meteor中的模板访问Iron Router参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆