在非权限页面上导航时如何防止路由并改为显示模态?
How to prevent routing and instead display a modal when navigation on a non-permission page?
在我的应用程序中,您可以在整个应用程序中随处找到许多 routerLinks。根据他们的角色,某些用户可能无法访问其中一些。我的 auth-guard-service 处理这种情况。
但在这些情况下,用户会被导航到禁止访问的页面,然后又返回到旧页面。
我想要的不是导航,而是在当前站点上显示模态或吐司消息(无论是什么)。
如何做到这一点?
这是我的一些代码:
export class AuthGuardService implements CanActivate {
constructor(public auth: AuthServiceProvider, public router: Router, private TokenHelper: TokenHelperService) {}
canActivate(Route: ActivatedRouteSnapshot): Observable<boolean> {
const expectedRoles = Route.data['authenticate'] as string[];
// ...... and so on (return true or false)
如果用户尝试激活的禁止页面位于延迟加载模块中,您可以通过放置 canLoad 保护来阻止导航。
{
path: 'admin',
loadChildren: () => import('./admin/admin.module').then(mod => mod.AdminModule),
canLoad: [AuthGuard]
},
=====
canLoad(route: Route): boolean {
return this.checkValidation();
}
checkValidation() {
if (authService.checkRights()) { return true}
else {
this.toastService.showToast('Forbiden');
return false
}
}
}
在我的应用程序中,您可以在整个应用程序中随处找到许多 routerLinks。根据他们的角色,某些用户可能无法访问其中一些。我的 auth-guard-service 处理这种情况。
但在这些情况下,用户会被导航到禁止访问的页面,然后又返回到旧页面。
我想要的不是导航,而是在当前站点上显示模态或吐司消息(无论是什么)。
如何做到这一点? 这是我的一些代码:
export class AuthGuardService implements CanActivate {
constructor(public auth: AuthServiceProvider, public router: Router, private TokenHelper: TokenHelperService) {}
canActivate(Route: ActivatedRouteSnapshot): Observable<boolean> {
const expectedRoles = Route.data['authenticate'] as string[];
// ...... and so on (return true or false)
如果用户尝试激活的禁止页面位于延迟加载模块中,您可以通过放置 canLoad 保护来阻止导航。
{
path: 'admin',
loadChildren: () => import('./admin/admin.module').then(mod => mod.AdminModule),
canLoad: [AuthGuard]
},
=====
canLoad(route: Route): boolean {
return this.checkValidation();
}
checkValidation() {
if (authService.checkRights()) { return true}
else {
this.toastService.showToast('Forbiden');
return false
}
}
}