我有以下路线定义.
export const Routes = RouterModule.forChild([
{
path: 'login',
component: LoginComponent
},
{
path: 'protected',
canActivate: [AuthGuardService],
component: ProtectedComponent
},
{
path: 'home',
component: HomeComponent,
canActivate: [AuthGuardService],
},
]);
我已成功实现AuthGuardService,如果用户未登录,则限制对受保护路由的访问.
我想要实现的是,如果用户已经登录并访问了登录路由,我希望它重定向到主页等其他路由.
解决方法:
如果我是你,我可能只是实现另一个与您的AuthGuardService完全相反的GuardService – 只允许用户f.e.本地存储中没有会话令牌.然后使用它来保护登录组件.
export const Routes = RouterModule.forChild([
{
path: 'login',
canActivate: [AnonymousGuardService],
component: LoginComponent
},
{
path: 'protected',
canActivate: [AuthGuardService],
component: ProtectedComponent
},
{
path: 'home',
component: HomeComponent,
canActivate: [AuthGuardService],
},
]);
原文地址:https://codeday.me/bug/20190713/1453104.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。