在我的Angular应用中,我有以下AuthGuardService
:
@Injectable({providedIn: 'root'})
export class AuthGuardService implements CanActivate, CanActivateChild {
constructor(private authService: AuthService, private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.authService.isAuthenticated()
.then((authenticated: boolean) => {
if (authenticated) {
return true;
} else {
this.router.navigate(['/']);
return false;
}
});
}
// === The following code will produce an error ===
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
this.canActivate(...arguments);
}
}
为了获得更简洁的代码,我想重用canActivate
方法,因此我在canActivateChild
内部调用了它。我想知道是否有一种方法可以将所有canActivateChild
的参数传递给canActivate
的调用。像这样:
this.canActivate(...arguments);
在Typescript中实现此目标的最佳方法是什么?谢谢!