我正在将 Meteor 与 FlowRouter 一起使用,但我遇到了一个奇怪的行为。我想知道我是否只是从根本上不理解某些东西。我有以下代码client/route.js
:
"use strict";
FlowRouter.route('/', {
name: 'home',
action: function() {
BlazeLayout.render('main', {main: "homePage"});
}
});
FlowRouter.route('/admin', {
name: 'admin',
triggersEnter: [isUserLoggedIn],
action: function() {
BlazeLayout.render('main', {main: "admin"});
}
});
function isUserLoggedIn() {
console.log("Worked");
if (Meteor.userId()) {
route = FlowRouter.current();
} else {
FlowRouter.go("home");
}
}
我跑meteor
了,然后去localhost:3000
查看控制台,我看到“Worked”,这意味着 isUserLoggedIn 函数已被触发。我没有点击 admin 或去localhost:3000/admin
. 只到顶层。为什么我没有去/admin
路由时会触发isUserLoggedIn函数?
编辑 1:看起来我的简化示例实际上工作得很好。实际的问题其实有点像这样:
"use strict";
FlowRouter.route('/', {
name: 'home',
action: function() {
BlazeLayout.render('main', {main: "homePage"});
}
});
FlowRouter.route('/admin', {
name: 'admin',
triggersEnter: [isUserLoggedIn(role)],
action: function() {
BlazeLayout.render('main', {main: "admin"});
}
});
function isUserLoggedIn(role) {
console.log("Worked");
if (Meteor.userId() && Role.userIsInRole(role)) {
route = FlowRouter.current();
} else {
FlowRouter.go("home");
}
}
通过 triggersEnter 传递参数似乎是不可能的(或者我不知道如何使其正常工作)。有没有办法通过 triggersEnter 发送参数?