【问题标题】:iron-router except fails?铁路由器除了失败?
【发布时间】:2015-02-14 01:41:05
【问题描述】:

所以我刚刚开始使用 Iron-router,我一直在构建一个登录系统。它在每条路线之前通过 .onBeforeAction 挂钩工作,检查用户是否已登录。但是,我希望公开一些路线,因此根据文档,我添加了一个 except 选项。除了问题是它不起作用:(有人能明白为什么吗?

Router.route('/new', function () {
  name: 'new',
  this.render('newComp');
});

Router.route('/c/:_id', { 
  name: 'compPage',
    data: function() { return Comps.findOne(this.params._id); }
});


Router.route('/c/:_id/embed', function () {
  name: 'embed',
  this.layout('empty'),
  this.render('compEmbed', {
    data: function () {
      return Comps.findOne({_id: this.params._id});
    }
  });
});

function loginFunction(){
  // all properties available in the route function
  // are also available here such as this.params

  if (!Meteor.user()) {
    // if the user is not logged in, render the Login template
    if (Meteor.loggingIn()) {
      this.render(this.loadingTemplate);
    } else {
      this.layout('empty');
      this.render('login');
    }
  } else {
    // otherwise don't hold up the rest of hooks or our route/action function
    this.next();
  }
}

Router.onBeforeAction( loginFunction, { 
  except: ['embed'] // this aint working
});

【问题讨论】:

    标签: meteor routing iron-router


    【解决方案1】:

    问题似乎出在您的路由定义中,名称参数应该在 Router.route() 的第三个参数中,就像这样(所以您的路由实际上没有名称,因此 except:['route.name'] 没有工作):

    Router.route('/c/:_id/embed', function () {
      this.layout('empty'),
      this.render('compEmbed', {
        data: function () {
          return Comps.findOne({_id: this.params._id});
        }
      });
    }, {
      name: 'embed',
    });
    

    更多关于命名路线的信息:http://eventedmind.github.io/iron-router/#named-routes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多