【问题标题】:Meteor FlowRouter triggersEnter triggering too earlyMeteor FlowRouter triggers进入触发太早
【发布时间】:2016-01-13 02:29:21
【问题描述】:

我正在将 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 函数已被触发。我没有点击管理员或去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 发送参数?

【问题讨论】:

    标签: javascript meteor meteor-blaze flow-router


    【解决方案1】:

    您需要从isUserLoggedIn 函数中返回一个函数,如下所示:

    function isUserLoggedIn (role) {
      return function (context, redirect, stop) {
        if (Meteor.userId() && Roles.userIsInRole(Meteor.userId(), role)) {
          route = FlowRouter.current();
        } else {
          FlowRouter.go("home");
        }
      }
    }
    

    【讨论】:

    • 到顶部!为我工作的唯一方法,但在第 3 行更新:if (Meteor.userId() && Roles.userIsInRole(Meteor.userId(), role)) {
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多