繁体   English   中英

在Meteor中的Iron Router的模板助手上使用参数

[英]Using params on template helpers from Iron Router in Meteor

我正在使用具有此特定路由的路由器:

Router.route('/organizations/:id', function () {
  this.render('organizationDetail', {id: this.params.id});
});

这将呈现“ organizationDetail”模板。

我有该模板的帮助器:

if (Meteor.isClient) {
  Template.organizationDetail.helpers({
    organization: function() {
        this.params.id????
        return FindById(id);
    }
  });
}

如何访问id变量以请求正确的对象?

您需要将该参数放入数据上下文中:

Router.route('/organizations/:id', function () {
  this.render('organizationDetail', {
      data: function() {
         return {id: this.params.id};
      }
  });
});

然后,您可以直接从此对象使用它, this对象将数据上下文保存在助手中:

if (Meteor.isClient) {
  Template.organizationDetail.helpers({
    organization: function() {
        return FindById(this.id);
    }
  });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM