[英]Meteor FlowRouter triggersEnter triggering too early
我將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
然后查看控制台,看到“已工作”,表示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發送參數?
您需要從isUserLoggedIn
函數返回一個函數,如下所示:
function isUserLoggedIn (role) {
return function (context, redirect, stop) {
if (Meteor.userId() && Roles.userIsInRole(Meteor.userId(), role)) {
route = FlowRouter.current();
} else {
FlowRouter.go("home");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.