簡體   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