在canActivate Guard中链接多个API的问题

时间:2019-09-13 10:46:22

标签: angular typescript angular-router

我遇到的情况是必须订阅多个API,并且从响应中可以确定天气是返回true还是false。在以下代码中,我尝试返回true,但是页面未重定向。

import { Injectable } from '@angular/core';
import { CanActivate, Resolve, Router, ActivatedRoute, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AuthService } from './auth.service';
@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {
  access = null;
  constructor(private _auth: AuthService, private _route: Router) { }

  // autheticate user logged in and router
  canActivate(
    act_route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ) {
    const token = localStorage.getItem('jwt_token');
    if (token) {
      if (!this._auth.isTokenExpired(token)) {
        if (act_route.data.userType) {
          this._auth.checkUserType().subscribe(
            res => {
              if (act_route.data.userType.includes(res)) {
                if (!act_route.data.permissionByPass && res !== 'SuperAdmin') {
                  this._auth.chkacs().subscribe(
                    chkres => {
                      this.access = chkres.data;
                      console.log('am/chkacs', chkres);
                      if (this.access[act_route.data.accessPage]) {
                        return true;
                      } else {
                        this._route.navigate(['403']);
                      }
                    }
                  );
                } else {
                  return true;
                }
              } else {
                this._route.navigate(['403']);
              }
            }
          );
        } else {
          console.log('false');
        }
      } else {
        this._auth.userOut();
        return false;
      }
    } else {
      this._auth.userOut();
      return false;
    }
  }
}

代码哪里出问题了?

0 个答案:

没有答案