Meteor Iron-Router在加载路由器之前,使用当前路由注册帮助程序

时间:2015-06-11 10:18:41

标签: javascript meteor iron-router meteor-helper

使用当前路由注册帮助程序会在控制台中返回错误:

Exception in template helper: TypeError: Cannot read property 'getName' of undefined

在路由器加载后 - 工作正常。 如何摆脱这个控制台错误?

帮助代码:

if (Meteor.isClient) {

  // create global {{route}} helper
  Handlebars.registerHelper('route', function () {
    return Router.current().route.getName();
  });

}

2 个答案:

答案 0 :(得分:2)

使用名为guarding的技术:

    // create global {{route}} helper
  Handlebars.registerHelper('route', function () {
    return Router.current() && 
           Router.current().route &&  
           Router.current().route.getName &&
           Router.current().route.getName();
  });

答案 1 :(得分:0)

您应该尝试在路线的onAfterAction挂钩中的模板数据中添加其他参数:

onAfterAction: function() {
  this.data.route = this.current().route.getName();
}

完成后,您可以使用yourTemplate.data.route

访问路线

代码未经测试。

相关问题