【问题标题】:Passing data from iron router into template helper将数据从 Iron 路由器传递到模板助手
【发布时间】:2015-04-01 00:08:49
【问题描述】:

我正在尝试将来自 Iron 路由器数据的 categoryId 传递到流星中的模板助手中。

这是我的路由器代码:

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 路由器中创建的 categoryId?

非常感谢您的帮助。

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

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

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

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

    Template.instance().data.categoryId;
    

    【讨论】:

    • 非常感谢您的工作。我的代码中还有另一个小问题,这使我无法意识到 this.categoryId 在我第一次尝试时就起作用了。我会在这里发帖以防其他人遇到这个问题。当我尝试使用它时,它正在将我的 categoryId 读取为字符串而不是数字。修复: var categoryId = parseInt(this.categoryId); var LessonData = Lessons.find({categoryId: categoryId}).fetch();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 2015-06-05
    • 2017-10-17
    • 1970-01-01
    • 2015-01-07
    相关资源
    最近更新 更多