我有以下可以激活路线守卫类
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