将数据从铁路由器传递到模板助手

时间:2015-03-31 15:38:57

标签: meteor iron-router

我正在尝试将类别ID从铁路由器数据传递到流星中的模板助手。

这是我的路由器代码:

Router.route('/lessons/:categoryId', function() {
this.subscribe('lessons');
this.render('Lessons', {
    data: {
        categoryId: this.params.categoryId
    }
});

这是我的模板代码:

Template.Lessons.helpers({
lessons: function () {
console.log('CategoryId: '+categoryId);
}
});

如何正确访问在铁路由器中创建的categoryId?

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

路由器中的

data为您的模板提供了上下文(this)。要从帮助者访问categoryId,请使用this.categoryId

Template.Lessons.helpers({
  lessons: function() {
    console.log('CategoryId: ' + this.categoryId);
  }
});

您还可以通过以下方式访问路由器数据:

Template.instance().data.categoryId;