当访问者尝试访问“提交”页面时,我会显示拒绝访问模板。这很好用。这是代码:
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 将引用窗口。您必须使用匿名函数作为 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();
}
});
}
关于javascript - 我使用的是 Iron :router v1. 0.7 但 this.render() 是 "undefined"。我看不出我做错了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28531904/