简体   繁体   English

FlowRouter.go(redirect)被触发,但实际上并未重定向

[英]FlowRouter.go(redirect) gets triggered, but does not actually redirect

FlowRouter.go(redirect); //gets triggered, but site does not actually redirect until refreshed.

I followed this guide to structure my routes: 我按照本指南来组织路线:

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' });
  },
};

The code above does the following: 上面的代码执行以下操作:

Case 1: Force Session.set("redirectAfterLogin", "/blah"); 情况1:强制Session.set(“ redirectAfterLogin”,“ / blah”);

  1. Logout of the app. 注销该应用程序。
  2. Enter in console Session.set("redirectAfterLogin", "/blah"); 在控制台中输入Session.set("redirectAfterLogin", "/blah");
  3. Login 登录
  4. Observe the following output in console: 在控制台中观察以下输出:
    • Accounts.onLogin()
    • redirect path exists
    • redirect is not welcome path, redirect to /blah

But I am still on the "Unauthorised" Layout, with "welcome" template". 但是我仍然处于“未授权”布局中,带有“欢迎”模板”。

  1. Press refresh, I get redirected to "Not Found" - this is the correct result. 按刷新,我将重定向到“未找到”-这是正确的结果。

Case 2: Session.get("redirectAfterLogin") is undefined 情况2:未定义Session.get(“ redirectAfterLogin”)

  1. Logout of the app. 注销该应用程序。
  2. Enter in console Session.set("redirectAfterLogin"); 在控制台Session.set("redirectAfterLogin");
  3. Login 登录
  4. Observe the following output in console: 在控制台中观察以下输出:
    • Accounts.onLogin()
    • no redirection, go root

But I am still on the "Unauthorised" Layout, with "welcome" template". 但是我仍然处于“未授权”布局中,带有“欢迎”模板”。

  1. Press refresh, I get redirected to "/" - this is the correct result. 按刷新,我将重定向到“ /”-这是正确的结果。

What exactly is hindering the logic here? 到底是什么在阻碍逻辑? Please help! 请帮忙!

I had this problem when trying to redirect after logout in an event: 在注销事件后尝试重定向时遇到了这个问题:

'click .js-logout'() {
    Meteor.logout();

    FlowRouter.go('signin');
},

My quick solution was to add timeout for calling router: 我的快速解决方案是为呼叫路由器添加超时:

'click .js-logout'() {
    Meteor.logout();

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

You might want to increase the timeout. 您可能要增加超时时间。

This was the reason for my problem, the roles was not subscribed properly and thus I was stuck on the unauthorised layout. 这就是导致我出现问题的原因,角色未正确订阅,因此我只能使用unauthorised布局。 I hope this helps! 我希望这有帮助!

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();
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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