代码之家  ›  专栏  ›  技术社区  ›  user9487981

执行逻辑时,can激活路由保护角度无法满足接口

  •  0
  • user9487981  · 技术社区  · 6 年前

    我有以下可以激活路线守卫类

    import {OnDestroy } from '@angular/core';
    import {
      CanActivate,
      ActivatedRouteSnapshot,
      RouterStateSnapshot,
      Router
    } from '@angular/router';
    import {Injectable} from '@angular/core';
    import {UserAuthorizationService} from "../userauthorizationservice/userauthorizationservice";
    
    
    @Injectable()
    export class ClientSuitsUserGuard implements CanActivate, OnDestroy{
      constructor(private userservice: UserAuthorizationService, private router: Router){}
      userservicesubscription;
      user ={
        id: null,
        isclient: false,
        issuitsviewer: false,
        issuitsadministrator: false,
        issuitssuperuser: false,
        isvenueuser: false
    
      };
    
      canActivate(route: ActivatedRouteSnapshot,
                  state: RouterStateSnapshot): boolean{
    
        this.userservicesubscription = this.userservice.receiveuser()
          .subscribe(
            (req: any)=>{
              if(req != null){
                this.user = req;
                if(this.user.isclient || this.user.issuitsviewer || this.user.issuitssuperuser || this.user.issuitsadministrator){
                  return true;
                }
                if(this.user == null ){
                  this.router.navigate(['/signin']);
                  return false;
                }else{
                  this.router.navigate(['/401']);
                  return false;
                }
              }
              this.router.navigate(['/signin']);
              return false;
            }
          );
      }
    
      ngOnDestroy(){
        this.userservicesubscription.unsubscribe();
      }
    }
    

    我不断地发现错误:

    /home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/frontend/suitsandtables/src/app/services/userservice/authguardservices/isclientserguard中出错。ts(31,44):声明类型既不是“void”也不是“any”的函数必须返回值。

    我希望route guard接收用户权限检查属性是否为true,并根据条件是否为false或true重定向到页面。

    我觉得我的代码是正确的,这是一个接口问题。我能做些什么来满足界面的要求吗?

    我做错了什么?

    我根据下面的答案做了以下编辑,但现在我不得不处理一个新的错误。

    canActivate(route: ActivatedRouteSnapshot,
                  state: RouterStateSnapshot): Observable<boolean>{
    
        return this.userservicesubscription = this.userservice.receiveuser()
          .map(
            (req: any)=>{
              if(req != null){
                this.user = req;
                if(this.user.isclient || this.user.issuitsviewer || this.user.issuitssuperuser || this.user.issuitsadministrator){
                  return true;
                }
                if(this.user == null ){
                  this.router.navigate(['/signin']);
                  return false;
                }else{
                  this.router.navigate(['/401']);
                  return false;
                }
              }
              this.router.navigate(['/signin']);
              return false;
            }
          );
      }
    

    运行时出现新错误:

    ERROR Error: Uncaught (in promise): TypeError: this.userservice.receiveuser(...).map is not a function
    TypeError: this.userservice.receiveuser(...).map is not a function
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Daniel W Strimpel Michael Dorgan    6 年前

    如我在上述评论中所述:

    subscribe方法中的return语句没有返回canActivate方法的值。您需要使用map方法并返回可观察链(将返回结果更新为可观察),而不是订阅。

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
      return this.userservice.receiveuser().map(
        (req: any)=>{
          if(req != null){
            this.user = req;
            if(this.user.isclient || this.user.issuitsviewer || this.user.issuitssuperuser || this.user.issuitsadministrator){
              return true;
            }
            if(this.user == null ){
              this.router.navigate(['/signin']);
              return false;
            }else{
              this.router.navigate(['/401']);
              return false;
            }
          }
          this.router.navigate(['/signin']);
          return false;
        }
      );
    }