将所有参数从一个Typescript方法传递到另一种的最佳方法是什么

问题描述 投票:0回答:1

在我的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中实现此目标的最佳方法是什么?谢谢!

angular typescript auth-guard
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.