Far*_*ruq 3 angular-routing angular angular-route-guards
我有一个使用 Angular 4 开发的现有项目。我需要根据用户权限控制对特定路由的访问。简化的路由配置如下所示:
[
{ path: '', redirectTo: '/myApp/home(secondary:xyz)', pathMatch: 'full' },
{ path: 'myApp'
children: [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', ... },
...
{ path: 'product'
children: [
{ path: '', redirectTo: 'categoryA', pathMatch: 'full' },
{ path: 'categoryA', component: CategoryAComponent, canActivate: [CategoryACanActivateGuard]},
{ path: 'categoryB', component: CategoryBComponent},
...
]
},
...
]
},
...
]
Run Code Online (Sandbox Code Playgroud)
现在,我想控制对www.myWeb.com/myApp/product/categoryA. 如果用户没有足够的权限,他/她将被重定向到... /product/CategoryB。我已经写了一个CanActivateRouteGuard 来做到这一点,守卫类看起来像这样:
import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot, ActivatedRoute } from '@angular/router';
import { MyService } from '... my-service.service';
@Injectable()
export class CategoryACanActivateGuard implements CanActivate {
constructor(private myService: MyService, private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
return this.myService.checkPermission()
.then(result => {
if (!result.hasAccess) {
//redirect here
this.router.navigate(['./myApp/product/categoryB']);
//works, but I want to remove hardcoding 'myApp'
//this.router.navigate(['../../categoryB']);
//doesn't work, redirects to home page
//this.router.navigate(['./categoryB'], { relativeTo: this.route});
//do not have this.route object. Injecting Activated route in the constructor did not solve the problem
}
return result.hasAccess;
});
}
}
Run Code Online (Sandbox Code Playgroud)
一切正常,但我想相对于目标路由重定向,如下所示:
this.router.navigate(['/product/categoryB'], { relativeTo: <route-of-categoryA>});
// or
this.router.navigate(['/categoryB'], { relativeTo: <route-of-categoryA>});
Run Code Online (Sandbox Code Playgroud)
不幸的是,relativeTo只接受ActivatedRoute对象,而我所拥有的只是ActivatedRouteSnapshotand RouterStateSnapshot。有没有办法相对于目标路线导航(在这种情况下categoryA)?任何帮助将不胜感激。
笔记:
this.router.navigateByUrl使用state.url. 我想用router.navigate([...], { relativeTo: this-is-what-need}).Far*_*ruq 11
事实证明,与Component等其他地方相比ActivatedRoute,RouteGuard 中注入的构造函数的工作方式有所不同。
在组件中,ActivatedRouteobject 指向激活该组件的路由。例如,在CategoryAComponent类中,以下代码将导航到CategoryB:
this.router.navigate([ '../CategoryB' ], { relativeTo: this.route });
Run Code Online (Sandbox Code Playgroud)
但是,上面的相同代码在RouteGuard添加到CategoryA路由配置的类中不起作用。在我的测试中,我发现构造函数注入的ActivatedRoute对象指向根路由。另一方面,ActivatedRouteSnapshot对象(在canActivate函数中作为参数传入)指向目标路径(在我的例子中categoryA)。但是,我们不能ActivatedRouteSnapshot在this.router.navigate(...)函数中传递这个对象。
我找不到更好的方法来解决这个问题,但以下代码对我有用:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
return this.myService.checkPermission()
.then(result => {
if (!result.hasAccess) {
//redirect here
let redirectTo = route.pathFromRoot
.filter(p => p !== route && p.url !== null && p.url.length > 0)
.reduce((arr, p) => arr.concat(p.url.map(u => u.path)), new Array<string>());
this.router.navigate(redirectTo.concat('categoryB'), { relativeTo: this.route });
}
return result.hasAccess;
});
}
Run Code Online (Sandbox Code Playgroud)
我可以为您提供一个更精简的解决方案:
import {
CanActivateFn,
UrlTree,
ActivatedRouteSnapshot,
RouterStateSnapshot,
createUrlTreeFromSnapshot
} from '@angular/router';
Run Code Online (Sandbox Code Playgroud)
const canActivate: CanActivateFn = (
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): true | UrlTree {
const urlTree = createUrlTreeFromSnapshot(route, ['categoryB']);
return result.hasAccess || urlTree;
}
Run Code Online (Sandbox Code Playgroud)
通过这个解决方案,您可以非常轻松地设置您可能需要的所有其他道具 - 命名插座导航、查询参数、片段......
小智 5
您可以使用 Angular 14 中的新createUrlTreeFromSnapshot()函数,如下所示:
const urlTree = createUrlTreeFromSnapshot(routeSnapshot, ['../categoryB']);
this.router.navigateByUrl(urlTree);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8902 次 |
| 最近记录: |