Iron-router and Meteor.user: 路由调用了两次
Iron-router and Meteor.user: route called two times
我正在使用 Iron 路由器的 onBeforeAction
方法来检查用户是否登录了除注册和登录路由之外的所有路由:
Router.onBeforeAction(function() {
if (!Meteor.user()) {
this.render('login');
}
else {
this.next();
}
}, {except: ['login', 'register']});
(仅供参考,登录过程是在登录 template/js 和 this code 中进行的)
这很好用,除了当我登录时,登录模板会在被实际模板替换之前快速显示。
当我检查发生了什么时,我发现 Meteor.user()
在 Iron 路由器准备好之前还没有准备好。结果,登录页面显示为用户未登录。然后(因为 Meteor.user()
是被动的),当 Meteor.user()
准备就绪时,路由将再次 运行,显示正确的模板。
我怎样才能让上面的这个小代码示例正常工作?(意思是:没有显示登录模板,因为 Meteor.user()
还没有准备好)
我还在 Stack Overflow 上发现我可以使用 fast-render package,但我不愿意破解渲染过程。 onRun()
方法也没有解决这个问题。
我还在某处读到我可以使用 Router.configure
的 waitOn
选项,但我得到的唯一示例是我还没有的订阅(我正在使用不安全的 auto发布包)。
我认为这不是最好的解决方案,但这个技巧很管用:
Router.onBeforeAction(function() {
if (Meteor.user() !== undefined) {
if (!Meteor.user()) {
this.render('login');
}
else {
this.next();
}
}
else {
this.pause();
}
}, {except: ['login', 'register']});
此代码示例有效,因为 Meteor.user()
在准备就绪之前是 undefined 并且它是 null 如果它准备好了但是有没有用户登录。
对于我的项目,我什至用加载屏幕替换了 this.pause()
:
Router.onBeforeAction(function() {
if (Meteor.user() !== undefined) {
if (!Meteor.user()) {
this.render('login');
}
else {
this.next();
}
}
else {
this.render('loading');
}
}, {except: ['login', 'register']});
我希望这会对其他人有所帮助。
我正在使用 Iron 路由器的 onBeforeAction
方法来检查用户是否登录了除注册和登录路由之外的所有路由:
Router.onBeforeAction(function() {
if (!Meteor.user()) {
this.render('login');
}
else {
this.next();
}
}, {except: ['login', 'register']});
(仅供参考,登录过程是在登录 template/js 和 this code 中进行的)
这很好用,除了当我登录时,登录模板会在被实际模板替换之前快速显示。
当我检查发生了什么时,我发现 Meteor.user()
在 Iron 路由器准备好之前还没有准备好。结果,登录页面显示为用户未登录。然后(因为 Meteor.user()
是被动的),当 Meteor.user()
准备就绪时,路由将再次 运行,显示正确的模板。
我怎样才能让上面的这个小代码示例正常工作?(意思是:没有显示登录模板,因为 Meteor.user()
还没有准备好)
我还在 Stack Overflow 上发现我可以使用 fast-render package,但我不愿意破解渲染过程。 onRun()
方法也没有解决这个问题。
我还在某处读到我可以使用 Router.configure
的 waitOn
选项,但我得到的唯一示例是我还没有的订阅(我正在使用不安全的 auto发布包)。
我认为这不是最好的解决方案,但这个技巧很管用:
Router.onBeforeAction(function() {
if (Meteor.user() !== undefined) {
if (!Meteor.user()) {
this.render('login');
}
else {
this.next();
}
}
else {
this.pause();
}
}, {except: ['login', 'register']});
此代码示例有效,因为 Meteor.user()
在准备就绪之前是 undefined 并且它是 null 如果它准备好了但是有没有用户登录。
对于我的项目,我什至用加载屏幕替换了 this.pause()
:
Router.onBeforeAction(function() {
if (Meteor.user() !== undefined) {
if (!Meteor.user()) {
this.render('login');
}
else {
this.next();
}
}
else {
this.render('loading');
}
}, {except: ['login', 'register']});
我希望这会对其他人有所帮助。