【问题标题】:FlowRouter.go(redirect) gets triggered, but does not actually redirectFlowRouter.go(redirect) 被触发,但实际上并没有重定向
【发布时间】:2018-04-01 10:14:52
【问题描述】:
FlowRouter.go(redirect); //gets triggered, but site does not actually redirect until refreshed.

我按照guide 来构建我的路线:

var authorised = FlowRouter.group();

var publicRoutes = FlowRouter.group();

FlowRouter.triggers.enter([isUserSignedIn]);

authorised.route('/',{
  name:'root',
  action(){
    BlazeLayout.render('App_body',{ main: 'App_home'});
  }
});

publicRoutes.route('/welcome',{
  name : 'welcome',
  action(){
    BlazeLayout.render('Unauthorised', { main: 'welcome' });
  }
});


function isUserSignedIn(){
  if (!Meteor.user() || Meteor.loggingIn()){
    var route = FlowRouter.current();
    if (route.path != "/welcome") {
      // Set Session to redirect path after login
      Session.set("redirectAfterLogin", route.path);
    }
    console.log("user is not signed in");
    FlowRouter.go('welcome');
  }
};

// Redirect After Login
Accounts.onLogin(function(){
  console.log("Accounts.onLogin()");
  var redirect = Session.get("redirectAfterLogin");
  if (redirect){
    console.log("redirect path exists")
    if(redirect != "/welcome"){
      console.log("redirect is not welcome path, redirect to ", redirect);
      FlowRouter.go(Session.get("redirectAfterLogin"));
    }
  }
  else{
    // if redirect doesn't exist, go "/"
    console.log("no redirection, go root");
    FlowRouter.go('root');
  }
})

// Not Found 
FlowRouter.notFound = {
  action() {
    BlazeLayout.render('Unauthorised', { main: 'App_notFound' });
  },
};

上面的代码做了以下事情:

案例1:强制Session.set("redirectAfterLogin", "/blah");

  1. 退出应用程序。
  2. 在控制台输入Session.set("redirectAfterLogin", "/blah");
  3. 登录
  4. 在控制台中观察以下输出:
    • Accounts.onLogin()
    • redirect path exists
    • redirect is not welcome path, redirect to /blah

但我仍然使用“未经授权”的布局,带有“欢迎”模板。

  1. 按刷新,我被重定向到“未找到” - 这是正确的结果。

案例2:Session.get("redirectAfterLogin") 未定义

  1. 退出应用程序。
  2. 在控制台输入Session.set("redirectAfterLogin");
  3. 登录
  4. 在控制台中观察以下输出:
    • Accounts.onLogin()
    • no redirection, go root

但我仍然使用“未经授权”的布局,带有“欢迎”模板。

  1. 按刷新,我被重定向到“/” - 这是正确的结果。

究竟是什么阻碍了这里的逻辑?请帮忙!

【问题讨论】:

    标签: javascript meteor flow-router meteor-useraccounts


    【解决方案1】:

    我在注销后尝试重定向时遇到了这个问题:

    'click .js-logout'() {
        Meteor.logout();
    
        FlowRouter.go('signin');
    },
    

    我的快速解决方案是为调用路由器添加超时:

    'click .js-logout'() {
        Meteor.logout();
    
        // we have to do redirect a bit later because logout is interfering the redirection
        setTimeout( 
          () => {
            FlowRouter.go('signin');
          }, 100
        );
     },
    

    您可能想要增加超时时间。

    【讨论】:

    • 看看我上面的解决方案,也许对你也有帮助!
    【解决方案2】:

    这是我的问题的原因,角色没有正确订阅,因此我被困在unauthorised 布局上。我希望这会有所帮助!

    FlowRouter.wait()
    // Tracker.autorun ->
    //   # if the roles subscription is ready, start routing
    //   # there are specific cases that this reruns, so we also check
    //   # that FlowRouter hasn't initalized already
    //   if Roles.subscription.ready() and !FlowRouter._initialized
    //      FlowRouter.initialize()
    Tracker.autorun(function(){
      if (Roles.subscription.ready() && !FlowRouter._initialized){
        FlowRouter.initialize();
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-29
      • 2010-12-04
      • 2019-10-02
      • 1970-01-01
      • 2021-07-04
      • 2017-07-30
      • 2021-04-23
      • 2021-11-03
      相关资源
      最近更新 更多