可以激活路由守卫 angular 执行逻辑时不能满足接口
can activate route guard angular cannot satisfy interface while executing logic
我有以下可以激活路由守卫class
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();
}
}
而且我不断收到错误消息:
ERROR in /home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/frontend/suitsandtables/src/app/services/userservice/authguardservices/isclientuserguard.ts (31,44): A function whose declared type is neither 'void' nor 'any' must return a value.
我希望路由守卫接收用户权限检查属性是否为真,并根据条件是否为真重定向到页面。
我觉得我的代码是正确的,这是一个界面问题。我可以做一些快速的调整来满足界面吗?
我做错了什么?
我根据下面的答案进行了以下编辑,但现在我不得不处理一个新的错误。
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
如我上面的评论所述:
Your return statements in the subscribe method is not returning values for your canActivate method. Instead of subscribing, you'll want to use the map method and return that observable chain (updating the return result to be an Observable).
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;
}
);
}
我有以下可以激活路由守卫class
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();
}
}
而且我不断收到错误消息:
ERROR in /home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/frontend/suitsandtables/src/app/services/userservice/authguardservices/isclientuserguard.ts (31,44): A function whose declared type is neither 'void' nor 'any' must return a value.
我希望路由守卫接收用户权限检查属性是否为真,并根据条件是否为真重定向到页面。
我觉得我的代码是正确的,这是一个界面问题。我可以做一些快速的调整来满足界面吗?
我做错了什么?
我根据下面的答案进行了以下编辑,但现在我不得不处理一个新的错误。
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
如我上面的评论所述:
Your return statements in the subscribe method is not returning values for your canActivate method. Instead of subscribing, you'll want to use the map method and return that observable chain (updating the return result to be an Observable).
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;
}
);
}