如何解决Angular 9 实现 canActive: Observable<boolean>,catchError ts 检查失败
我使用 angular9,rxjs6 并实现 canActive: Observable
但是我在使用 catchError 时出现错误,如下所示:
我该如何解决这个问题?
解决方法
在你的守卫中解决 canActivate 函数的另一种方法是只使用 subscribe 因为我认为在这种情况下使用 map 是错误的
this.yourService.subscribe(
(r) => {
//your code
return true;
},(error: any) => {
return false;
}
);
,
canActivate 需要返回一个完成的 Observable:
// ...
.pipe(catchError(error => {
// your work to navigate or whatever
})).first(); // this might not be necessary - ensure `first` is imported if you use it
缺少返回值并使用 map() 而不是 subscribe() 因为 subscribe() 返回的是 Subscription 而不是 Observable
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。