有没有办法在 authguard 中使用相对路径模式进行重定向?
我试过
@Injectable()
export class ServerAuthGuard implements CanActivate {
constructor(private _router: Router,
private _route: ActivatedRoute) {
}
canActivate(route: ActivatedRouteSnapshot): boolean {
this._router.navigate(['../../servers/'], {relativeTo: this._route});
return false;
}
}
它应该从 /projects/2/servers/71
重定向到 /projects/2/servers/
但它总是将它重定向到 /servers
(当我在一个组件中做同样的事情时它工作正常)。
最佳答案
relativeTo 也应该接受一个 ActivatedRouteSnapshot,看起来这可能被忽略了,同时这里有一个解决方法:
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
// if(...) {
const segments = state.url.split('/');
segments.pop();
segments.shift();
this.router.navigate(segments);
// } else {
// return true;
// }
}
关于Angular 2 authguard 相对重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42200849/