如何在canActivate Guard中订阅两个API

时间:2019-09-13 19:59:47

标签: angular

我正在尝试将两个可观察对象链接到一个canActivate Guard中。 但是我在vs代码中遇到了以下错误:

Type 'Observable<boolean | Observable<boolean> | Promise<boolean>>' 
is not assignable to type 'boolean | Observable<boolean> | Promise<boolean>'.

以下是我的代码:

(忽略我订阅相同的api的事实,因为它暂时对我不可用。但是实现方法是相同的。)

canActivate(
    act_route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean> | Promise<boolean> | boolean {
    const token = localStorage.getItem('jwt_token');
    if (token) {
      if (!this._auth.isTokenExpired(token)) {
        if (act_route.data.userType) {
          return this.checkUserType(act_route);
        } else {
          return true;
        }
      } else {
        this._auth.userOut();
        return false;
      }
    } else {
      this._auth.userOut();
      return false;
    }
  }

  checkUserType(act_route: any): Observable<boolean> | Promise<boolean> | boolean {
    return this._auth.checkUserType().pipe(
      map(
        res => {
          if (act_route.data.userType.includes(res)) {
            if (!act_route.data.permissionByPass && res !== 'SuperAdmin') {
              return this.checkAccess(act_route);
            } else {
              return true;
            }
          } else {
            this._route.navigate(['403']);
            return false;
          }
        }
      )
    );
  }

  checkAccess(act_route: any): Observable<boolean> | Promise<boolean> | boolean {
    return this._auth.checkUserType().pipe(
      map(
        res => {
          if (act_route.data.userType.includes(res)) {
            if (!act_route.data.permissionByPass && res !== 'SuperAdmin') {
              return true;
            } else {
              return true;
            }
          } else {
            this._route.navigate(['403']);
            return false;
          }
        }
      )
    );
  }

我该怎么办?我要去哪里错了?

0 个答案:

没有答案