我有一个流星应用程序和包iron-router
,如果除少数页面之外没有连接用户,我会尝试阻止所有页面。如果没有指定任何内容,我们将进入登录页面。
所以在文件router.js中,我有:
Router.onBeforeAction(function() {
if (!Meteor.userId()) {
Router.go('login');
} else {
this.next();
}
}, {
except: [
"login", "landing", "register", "forgotPassword"
]
});
Router.route('/', function () {
Router.go('landing');
});
但是当我继续localhost:3000/
时,我会重定向到登录页面而不是登录页面。
如果我删除onBeforeAction功能,我会重定向到目标网页。 所以这两个功能一定是个问题,但我不知道在哪里。也许我需要精确的" /"在例外情况下但它不起作用。你有什么想法吗?
答案 0 :(得分:0)
您还需要在例外中定义路线'/'
,否则会被onBeforeAction
尝试重新定义如下
Router.onBeforeAction(function() {
if (!Meteor.userId()) {
Router.go('login');
} else {
this.next();
}
}, {
except: [
"default", "login", "landing", "register", "forgotPassword"
]
});
Router.route('/', function () {
Router.go('landing');
}, {
name: "default"
} );
在这种情况下,您可以为路线命名,然后将其添加到例外列表