[英]Role based redirecting
I want to redirect user depending on his role. 我想根据用户的角色来重定向用户。 Fe When user entered as 'Teacher', he is redirected on page '1'; Fe当用户输入为“教师”时,他将在页面“ 1”上重定向; if he entered as 'Student', he is redirected on page '2'. 如果他以“学生”身份输入,则会在页面“ 2”上重定向。 It has also to work when user inputs only main url 'app.com' (Fe) 当用户仅输入主网址“ app.com”(Fe)时,它也必须工作
const routes: Routes = [
{
path: '',
component: TesLandingPageContainer,
canActivate: [AutheticatedGuard],
canActivateChild: [UserRoleGuard],
children: [
{
path: '',
redirectTo: '/users/profile',
pathMatch: 'full',
},
...
},
{
path: '**',
redirectTo: ''
}
];
I have this guard 我有这个警卫
@Injectable()
export class AutheticatedGuard implements CanActivate {
constructor(private store: Store<AppState>, private router: Router) { }
canActivate(): Observable<boolean> {
return this.store.pipe(
select(getIsAuthenticated),
filter(isAuth => isAuth != null),
tap(isAuth => {
if (!isAuth) {
this.router.navigate(['/login']);
}
}),
take(1)
);
}
}
If I write so, it loops 如果我这样写,它会循环
canActivate(): Observable<boolean> {
return this.store.pipe(
select(getUserRoleAndAuth),
filter(user => user.isAuth != null),
tap(user => {
switch (user.role) {
case 'ADMIN': {
this.router.navigate(['user/requests']);
break;
}
case 'TEACHER': {
this.router.navigate(['groups']);
break;
}
case 'STUDENT': {
this.router.navigate(['student']);
break;
}
default: {
this.router.navigate(['/login']);
}
}
}),
map(user => user.isAuth),
take(1)
);
```
Instead of using this.router.navigate(['student']);
而不是使用this.router.navigate(['student']);
use return this.router.parseUrl('/student');
使用return this.router.parseUrl('/student');
parseUrl
will create UrlTree
which help to navigate application from CanActivate
. parseUrl
将创建UrlTree
,以帮助从CanActivate
导航应用程序。
you need to return somthing from CanActivate
interface , either boolean or UrlTree. 您需要从CanActivate
接口(布尔值或UrlTree)返回东西。
As per your logic return either student or teacher URL. 根据您的逻辑,返回学生或老师的URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.