我在一个警卫中使用canactivate方法。在我的内部有一个可观测的,它给了我一个物体。此对象有密码。我检查密码,如果正确,我希望canactivate返回true,否则返回false。
这是密码
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const uid = route.params.userId;
const hackId = route.params.hackId;
const token = this.authService.token;
const queryParamsPassword = route.queryParams.password;
this.hacksStorageService.fetchHack(token, uid, hackId)
.subscribe(
hack => {
const hackPassword = hack[0].password;
if (queryParamsPassword === hackPassword ) {
return true
} else {
this.router.navigate(["/"]);
return false
}
},
error => {
return false;
}
)
}
我很难理解他们的反应。我必须在任何时候做出回应。一旦我从订阅中获得了值,那么我想返回canactivate中的布尔值。
我得到的错误在canActivate方法上(红色)
[ts]
Property 'canActivate' in type 'RoleGuard' is not assignable to the same property in base type 'CanActivate'.
Type '(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => void' is not assignable to type '(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => boolean | Observable<boolean> | Pr...'.
Type 'void' is not assignable to type 'boolean | Observable<boolean> | Promise<boolean>'.
我以为我会回到中情局的每一个部门,所以这应该是足够的,但显然不是。如果我退回订阅,他也会抱怨。
有什么帮助吗?谢谢。