Meteor Iron-Router 在加载路由器之前使用当前路由运行注册助手

Meteor Iron-Router Registering helper with current route runs before router is loaded

使用当前路由注册助手 returns 控制台错误:

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

}

您应该尝试在路由的 onAfterAction 钩子中的模板数据中添加一个附加参数:

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

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

访问您的路线

代码未经测试。

使用名为 guarding 的技术:

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