angular - Angular 6 RXJS 过滤器等待订阅中的布尔值
问题描述
我正在尝试检查用户是否已通过身份验证,并且我正在使用一种方法,如果用户已通过身份验证,则该方法返回 true 或 false 的可观察值。我正在尝试做的是等待授权有一个值,然后在回调函数中检查它是真还是假,并根据它的值执行操作。所以基本上我不希望我的过滤器函数检查值是真还是假,而是在继续订阅回调中的代码之前检查是否有值。
这是我的功能:
this.oidcSecurityService.getIsAuthorized().pipe(filter(authorized => authorized !== undefined)).subscribe(authorized => {
console.log(authorized);
this.isAuthorized = authorized;
if (!authorized) {
debugger
this.login()
}
else {
debugger
this.getBoSpar()
}
});
我做错了什么,如何让过滤器功能检查是否已获取授权,而不是它是真还是假?我没有收到任何错误,只是授权总是被评估为假,即使我知道用户已经过身份验证(我已经分离了发生在点击事件上的逻辑,并且有效)。
解决方案
authorized !== undefined
看起来不对
authorized != null
仅检查 null 和 undefined
this.oidcSecurityService.getIsAuthorized()
.pipe(
tap(authorized => console.log('BEFORE', authorized)),
filter(authorized => authorized != null),
tap(authorized => console.log('AFTER', authorized))
)
.subscribe(authorized => {
console.log(authorized);
this.isAuthorized = authorized;
if (!authorized) {
debugger
this.login()
} else {
debugger
this.getBoSpar()
}
});
值得使用抽头检查过滤器前后通过管道的值。
推荐阅读
- html - Angular 4 - 在 HTML 模板中添加日期到日期
- c# - 将新项目添加到列表中以替换列表中的旧项目
- excel - 共享工作簿的列宽不变
- java - 递归检索二叉搜索树的子集
- android - 在颤动中打开位置后如何更新我的位置
- ios - 使用 Intune SDK 访问 MS Graph 资源
- c# - 如何确定向网站发出的请求是来自原始 IP 还是被匿名代理服务器屏蔽?
- html - HTML/CSS 如何使用 grid-template 属性
- .net - C# 中的 db4o 本机查询:如何按第一个数字查找?
- amazon-web-services - AWS SNS 排队行为