Iron Router onBeforeAction没有被调用 [英] Iron Router onBeforeAction isn't being called
问题描述
我设置了/user
路由,如果当前用户未登录,该路由应该呈现login
模板.整个路由器都有一个waitOn
等待currentUser
订阅结束.问题是当我转到/user
时,它只是呈现了dataNotFound
模板.
I have a /user
route set up, which is supposed to render the login
template if the current user isn't logged in. The entire router has a waitOn
that waits for the currentUser
subscription to finish. The problem is that when I go to /user
it simply renders the dataNotFound
template instead.
以下是与这种情况相关的代码段.我一直很小心地按照它们在我的lib/router.js
文件中定义的顺序向您显示.
Here's the snippets of code that are relevant to this situation. I've been careful to show you them in the order they're defined in my lib/router.js
file.
Router.plugin('dataNotFound', {notFoundTemplate: 'notFound'});
Router.onBeforeAction(function () {
console.log(Meteor.userId())
if (!Meteor.userId()) this.render('login');
else this.next();
}, { only: ['user'] });
Router.configure({
waitOn: function () { return Meteor.subscribe('currentUser'); }
});
Router.route('/user', {
name: 'user',
template: 'userView',
data: function () { return Meteor.user(); }
});
上面的console.log
甚至不会触发.在我看来,由于即使应该最初呈现dataNotFound
也是应该是一个反应函数,所以之后不久应该触发onBeforeAction
并呈现login
模板,对吧?
That console.log
above doesn't even ever fire. It seems to me that since it should be a reactive function that even if initially the dataNotFound
is rendered, then soon after that the onBeforeAction
should be fired and render the login
template, right?
推荐答案
从流星0.9.1升级到1后,onBeforeAction挂钩出现问题. 它没有在应有的时候被解雇.就像注销后一样,我手动输入地址,而不是登录页面,我看到的是实际站点在等待从未出现的数据. onRun挂钩解决了它,但正如文档所说,它只被触发了一次.
I had a problem with onBeforeAction hook after upgrading from meteor 0.9.1 to 1. It didnt get fired when it should. Like after I log out, I enter address manually and instead of login page I can see the actual site waiting for data that never comes. onRun hook solved it, but as docs says it gets fired only once.
Router.onRun(function () {
if (!Meteor.userId()) {
this.render('login');
} else {
this.next();
}
}, { only: ['user'] });
这篇关于Iron Router onBeforeAction没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!