首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rxjs combineLatest中的角ngrx和combineLatest不工作

Rxjs combineLatest中的角ngrx和combineLatest不工作
EN

Stack Overflow用户
提问于 2021-08-03 15:14:40
回答 2查看 257关注 0票数 0

我遇到了一个我无法解决的问题:

代码语言:javascript
复制
onSelectCompany() {
 combineLatest([this.idCompany$, this.idUser$, this.router.events]).subscribe(res => {
   if(res[2] instanceOf NavigationEnd){
     this.router.navigateByUrl(`get-info/${res[0]/res[1]`)
   }
 })
}

我在我的组件的ngOnInit上调用这个函数,我有两个可观察的函数,我需要像我的url那样的参数,只有当我没有刷新页面时,我才需要在这个url上导航。但是我编写的函数不起作用,我没有错误,但是在调试过程中,执行不会进入订阅。为什么?

EN

回答 2

Stack Overflow用户

发布于 2021-08-03 15:21:23

在执行该函数之后,所有3个可观察到的对象都必须发出一些信息。

来源:https://www.learnrxjs.io/learn-rxjs/operators/combination/combinelatest

代码语言:javascript
复制
Be aware that combineLatest will not emit an initial value 
until each observable emits at least one value.

1解决方案可以是将函数从ngOnInit转移到字段级别或构造函数。

票数 0
EN

Stack Overflow用户

发布于 2021-08-04 06:25:12

您可以通过使用startWith操作符来模拟路由器事件来绕过它。如果您对combineLatest结果进行重构,而不是使用索引访问它,那么对您来说也会更容易。

代码语言:javascript
复制
onSelectCompany() {
 const helper$ = this.router.events.pipe(startWith(null)); // or some other 'safe' value
 combineLatest([this.idCompany$, this.idUser$, helper$])
 .subscribe(([ companyId, userId, routerEvent ]) => {
 // destructure the res array and access the values with variable names
   if(routerEvent instanceOf NavigationEnd){
     this.router.navigateByUrl(`get-info/${companyId}/${userId}`)
   }
 })
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68638633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档