繁体   English   中英

流星FlowRouter触发器输入为时过早触发

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM