【问题标题】:iron router syntax: onAfterAction铁路由语法:onAfterAction
【发布时间】:2015-06-26 04:45:21
【问题描述】:
Router.route('/courses/:_catalog', function () {
  var courseCatalog = this.params._catalog.toUpperCase();

  Meteor.subscribe("courseCatalog", courseCatalog);

  this.render('CourseDetail', {
    to: 'content',
    data: function () {
      return Courses.findOne({catalog: courseCatalog});
    }
  });
}, {
  onAfterAction: function() {
    if (!Meteor.isClient) {
      return;
    }
    debugger
    var course = this.data(); <======
    SEO.set({
      title: "course.catalog"
    });
  }
});

在上面的代码中,请看debugger语句。我想访问数据,但似乎我做错了,因为this.data 不存在。我也尝试了Courses.find().fetch(),但我在onAfterAction 中只得到了一个空数组。什么是正确的语法,我错过了什么?

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

    它需要在 this.ready() 块内:

      onAfterAction: function() {
        if (this.ready()) {
          var course = this.data(); 
          ...
        }
      }

    【讨论】:

      【解决方案2】:

      您需要先订阅数据。看看 waitOn 函数来做到这一点。服务器只发送你订阅的文档,由于你没有订阅,Courses.find().fetch() 返回一个空数组。

      另外,不要将 SEO 内容放在 onAfterAction 中。把它放在保证只运行一次的onRun中。

      【讨论】:

      • 对不起,我已经订阅了数据,但错误地将其遗漏在问题中。所以这似乎不是问题的原因。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 2017-08-15
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多