【问题标题】:Is there a way to access Iron Router parameter from template in Meteor有没有办法从 Meteor 中的模板访问 Iron Router 参数
【发布时间】:2015-10-30 12:02:44
【问题描述】:

我有一个包含参数的路由,我需要从许多不同的模板访问它。下面是路由的一个例子,有几条路由在 _occasionnId 参数改变后非常相似:

例如:

路线 1:/occasions/:_occasionId/cards/

路由器 2:/occasions/:_occasionId/tables/

这是我对每条路线的完整代码,唯一真正改变的是路线路径和模板。

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 参数放入具有导航的模板中,该导航进入所有这些页面。我的目标是从 Route 1 到 Router 2。但我不知道如何在模板中添加正确的 URL。

我的模板是:

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

在“:_occasionId”的“occasionnav”模板中,我需要将其与当前正在查看的页面相同的参数插入此处。

如果有人对解决此问题的最佳方法有任何见解或建议,我将不胜感激。

【问题讨论】:

    标签: meteor iron-router meteor-helper


    【解决方案1】:

    如果您想在 Meteor 应用程序中呈现内部路由,我建议使用 {{pathFor}}

    您只需要设置正确的上下文并命名您的路线,例如:

    <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 获取模板中的路由器参数。

    【讨论】:

      【解决方案2】:

      您可以将 _occasionId 作为模板帮助器传递,并以如下方式渲染它:

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

      【讨论】:

      • 谢谢,但我似乎无法让它工作。我尝试将它作为模板助手传递,它仅在我第一次加载页面时显示。当页面刷新时它会消失。我是通过 session.set 和 'Handlebars.registerHelper('currentOccassion', function() { return Session.get("currentOccassion"); });'
      • 可能是您使用非反应变量设置了模板助手。您可以将 Session.get("currentOcassion") 放入该助手中。它也可以工作。
      【解决方案3】:

      你应该试试:

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

      当你使用 Router.map 时,它的语法并不完全相同:

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

      您可以像在模板中那样访问:

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

      【讨论】:

      • 感谢您的建议,但在第 83 行出现了意外 = 即这一行:currentoccasion = this.params._occasionId;
      • 好的,你可以试试这个吗? this.layout("LayoutName"); this.render('cards', {data: {currentoccasion = this.params._occasionId}});
      猜你喜欢
      • 2016-01-04
      • 2014-05-26
      • 2014-05-19
      • 2015-08-10
      • 1970-01-01
      • 2015-04-11
      • 2016-09-24
      • 2015-07-07
      • 2014-01-04
      相关资源
      最近更新 更多