Javascript 基于权限的网络路由
我正在使用angular 6创建一个项目。在我的项目中,基于权限和角色的重定向。我已经用activate方法实现了这一点,它可以在除一个之外的所有情况下工作 问题是登录页也是基于权限的,如果用户没有权限,登录页就会失败Javascript 基于权限的网络路由,javascript,angular,Javascript,Angular,我正在使用angular 6创建一个项目。在我的项目中,基于权限和角色的重定向。我已经用activate方法实现了这一点,它可以在除一个之外的所有情况下工作 问题是登录页也是基于权限的,如果用户没有权限,登录页就会失败 { path: '', redirectTo: 'user-management', pathMatch: 'full' }, { path: 'user-management', component: UserManagmentComponent, canA
{ path: '', redirectTo: 'user-management', pathMatch: 'full' },
{ path: 'user-management', component: UserManagmentComponent, canActivate:[AuthGuard], data: {permisson:'im_user_view'}},
{ path: 'test-profile', component: TestProfileComponent, canActivate:[AuthGuard], data: {permisson:'ucr_test_profile_view'}},
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable < boolean | UrlTree > | Promise < boolean | UrlTree > | boolean | UrlTree {
let perm = next.data.permisson || null;
return this.getUserInfo(perm)
return true;
}
getUserInfo(perm): Observable < boolean > {
if (!perm) {
return of(false);
}
return this.commonService.getUserLoggedIn().pipe(map(result => {
if (this.PermissionService.checkPermission(perm)) {
return true
} else {
return false;
}
})
}
{path:'',重定向到:'user management',pathMatch:'full'},
{path:'user management',组件:usermanagementcomponent,canActivate:[AuthGuard],数据:{permisson:'im_user_view'},
{path:'testprofile',component:TestProfileComponent,canActivate:[AuthGuard],数据:{permisson:'ucr\u test\u profile\u view'},
激活(
下一步:ActivatedRouteSnapshot,
状态:RouterStateSnashot):可观察的| Promise| boolean | UrlTree{
设perm=next.data.permisson | | null;
返回此.getUserInfo(perm)
返回true;
}
getUserInfo(perm):可观察{
如果(!perm){
归还(假);
}
返回这个.commonService.getUserLoggedIn().pipe(映射(结果=>{
if(this.PermissionService.checkPermission(perm)){
返回真值
}否则{
返回false;
}
})
}
您有问题吗?是的,如何处理登录页的权限嗯,我看不到任何登录页路由。另外,您可以改进代码段格式吗?@Humberd:{path:'',重定向到:'user management',pathMatch:'full'},重定向只是重定向到另一个视图。而另一个视图有一个保护。因此,如果您想在没有保护的情况下拥有相同的视图,则必须重新定义它:{path:'',component:usermanagementcomponent}