2017-10-20 41 views
1
FlowRouter.go(redirect); //gets triggered, but site does not actually redirect until refreshed. 

我跟着这个guide构建我的路线:FlowRouter.go(重定向)被触发,但实际上并不重定向

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. 按刷新按钮,我将重定向到“/” - 这是正确的结果。

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

回答

0

我想注销后重定向在一个事件时,这个问题:

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

您可能希望增加超时。

+0

看看我上面的解决方案,它可能也会帮助你! – user2587676

0

这是我的问题的原因,角色没有正确订阅,因此我被卡在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(); 
    } 
});