我使用的是 iron:router v1.0.7,但 this.render() 是 "undefined"。我看不出我做错了什么

I'm using iron:router v1.0.7 but this.render() is "undefined". I can't see what I'm doing wrong

当访问者试图访问“提交”页面时,我显示了一个访问被拒绝的模板。这很好用。这是代码:

Router.route('/', {name: 'etoeventsList'});
Router.route('/submit', {name: 'etoeventSubmit'});

var requireLogin = function () {
  if (!Meteor.user()) {
    this.render('accessDenied');
  } else {
    this.next();
  }
};

Router.onBeforeAction(requireLogin, {only: 'etoeventSubmit'});

我想在不同的上下文(匿名用户访问“/”)下使用 "requireLogin",所以我想我会添加一个参数以允许我传入要呈现的模板。像这样:

var requireLogin = function (template) { // now with argument 'template'
  if (!Meteor.user()) {
    this.render(template); // using 'template'
  } else {
    this.next();
  }
};

Router.onBeforeAction(requireLogin('accessDenied'), {only: 'etoeventSubmit'}); // passing 'accessDenied'
Router.onBeforeAction(requireLogin('index'), {only: 'etoeventsList'}); // passing 'index'

我收到的错误是Uncaught TypeError: undefined is not a function,我想显示的模板没有显示。

你不能那样做。按照您的操作方式,this 将引用 Window。您将不得不使用匿名函数作为 onBeforeAction.

的参数
Router.configure({
    layoutTemplate: 'layout',
    loadingTemplate: 'loading',
    notFoundTemplate: 'notFound',
    waitOn: function() {

    }
});

if(Meteor.isClient) {
  Router.onBeforeAction(function() {
    // is user logged in
    if (! Meteor.user()) {
      if (Meteor.loggingIn()) {
        this.render('loading');
      } else {
        this.render('accessDenied');
      }
    } else {
      this.next();
    }
  });
}