angular - 创建一个订阅,一旦捕获到一个有效的 authState,就会停止“轮询”
问题描述
是否可以创建一个尝试反复获取用户身份验证状态的订阅,直到user != null
,一旦出现这种情况,它就会停止:
ngOnInit() {
this.isMobile = this.breakpointObserver.observe([Breakpoints.Handset]);
this.authSubscription = this.afAuth.authState.subscribe(user => {
if(user) {
localStorage.setItem('uid', user.uid);
this.store.dispatch(userActions.getData());
}
})
}
控制台输出如下导致浏览器崩溃:
我的操作如下:
@Effect()
getData$ = this.actions$.pipe(
ofType(UserActions.getData),
switchMap(() => {
return this.userService.getUserById(localStorage.getItem('uid')).pipe(
map(
(data: IUser) => UserActions.dataReceived({ payload: UserService.parseData(data) })
)
);
}
)
);
解决方案
您可以使用takeUntil()
运算符 和的组合interval()
。
我更喜欢这种解决方案,因为您不会一个接一个地触发请求 1,您需要在两者之间留出一些时间。
推荐阅读
- idris - 尝试为两人游戏键入决策树
- android - Androidx 最小 SDK
- node.js - 即使使用看似正确的处理程序,对我的 App Engine 网站的请求也不会重定向到 HTTPS
- ruby-on-rails - 我需要安装任何特定的生成器,以便设计可以在 ruby on rails 中工作吗?
- java - Java String == 运算符与字符串文字
- javascript - Rails Google Maps Turblinks 在生产模式下不工作
- c++ - 虚幻引擎 - 如何通过 C++ 代码获取 AxisMappings
- docker - docker-compose:EXPOSE 在主机端口或容器端口上起作用吗?
- javascript - 如何使用索引在 Vuejs (vuetify) 中仅选择一个扩展面板?
- bash - 排序命令(MacOS 终端)给出不一致的结果