0

我有以下可以激活路由保护类

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/isclientuserguard.ts (31,44) 中的错误:声明类型既不是“void”也不是“的函数any' 必须返回一个值。

我希望路由守卫接收用户权限检查属性是否为真,并根据条件为假或真重定向到页面。

我觉得我的代码是正确的,这是一个界面问题。我可以做些什么来满足界面?

我究竟做错了什么?

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

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
4

1 回答 1

0

正如我在上面的评论中所说:

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;
    }
  );
}
于 2018-05-09T23:34:07.987 回答