将数据从 Iron Router 传递到模板助手

Passing data from iron router into template helper

我正在尝试将 iron 路由器数据中的 categoryId 传递到 meteor 中的模板助手中。

这是我的路由器代码:

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

如何正确访问在iron router中创建的categoryId?

非常感谢您的帮助。

来自路由器的

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

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

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

Template.instance().data.categoryId;