我在Meteor JS应用程序中为我的铁路由器signOut路由提供了以下代码。我试图将已弃用的Router.map转换为新的Router.route语法,但很难让onBeforeAction和onAfterAction工作。以下代码块的正确Router.route语法是什么?
Router.map(function () {
// sign-out the user then redirect them to the home page
this.route('signOut', {
path: '/sign-out',
onBeforeAction: function () {
if (Meteor.userId()) {
Meteor.logout()
}
this.next();
},
onAfterAction: function () {
this.redirect('/');
}
});
});
答案 0 :(得分:2)
Router.route('/sign-out', function() {
//here you put things you wanna render, it's empty since you just want to logout and redirect
}, {
name: 'signOut',
onBeforeAction: function () {
if (Meteor.userId()) {
Meteor.logout()
}
this.next();
},
onAfterAction: function () {
Router.go('/');
}
});
我认为你应该添加waitOn函数,因为如果没有先前订阅,可能首先没有Meteor.user()对象