我遇到了一个我无法解决的问题:
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上导航。但是我编写的函数不起作用,我没有错误,但是在调试过程中,执行不会进入订阅。为什么?
发布于 2021-08-03 15:21:23
在执行该函数之后,所有3个可观察到的对象都必须发出一些信息。
来源:https://www.learnrxjs.io/learn-rxjs/operators/combination/combinelatest
Be aware that combineLatest will not emit an initial value
until each observable emits at least one value.1解决方案可以是将函数从ngOnInit转移到字段级别或构造函数。
发布于 2021-08-04 06:25:12
您可以通过使用startWith操作符来模拟路由器事件来绕过它。如果您对combineLatest结果进行重构,而不是使用索引访问它,那么对您来说也会更容易。
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}`)
}
})
}https://stackoverflow.com/questions/68638633
复制相似问题