FlowRouter重定向如果用户登录,如果路径是
问题描述:
我使用流星与FlowRouter,我在寻找这样一个条件:FlowRouter重定向如果用户登录,如果路径是
- 如果用户登录& &如果访问路径http://x.x.x.x/
- 然后重定向到http://x.x.x.x/clients
我目前的路线:
Accounts.onLogin(function(){
FlowRouter.go('clients');
});
Accounts.onLogout(function(){
FlowRouter.go('home')
});
FlowRouter.triggers.enter([function(context, redirect){
if(!Meteor.userId()){
FlowRouter.go('home')
}
}]);
FlowRouter.route('/', {
name: 'home',
action(){
BlazeLayout.render('HomeLayout');
}
});
FlowRouter.route('/clients',{
name: 'clients',
action(){
BlazeLayout.render('MainLayout', {main: 'Clients'});
}
});
答
if(Meteor.userId() && FlowRouter.getRouteName() === 'route_name'){
FlowRouter.go('/route_name');
}
在流路由器文档中,有一些是获取当前路由,如果您需要重新构造上述语句。 https://github.com/kadirahq/flow-router/blob/master/README.md
答
我会说,你只需要改变你的FlowRouter.route(“/” ...)配置了一下:
FlowRouter.route('/', {
triggersEnter: [function(context, redirect) {
if (Meteor.userId()) {
redirect('/clients');
}
}],
name: 'home',
action(){
BlazeLayout.render('HomeLayout');
}
});
因此,任何登录的用户访问“/”来被重定向到'客户' - 在我测试时工作得很好。以下是流路由器文档中的一些背景信息:https://github.com/kadirahq/flow-router/blob/master/README.md#redirecting-with-triggers