如何解决在canActivateChild返回true / false之前,正确等待多个可观察对象返回路由防护中的数据
我的canActivateChild路由防护出现问题,在确定是否可以激活路由之前,它不等待数据从3个服务调用返回到后端。我已经将它挂接到ngRx存储中并正在调度3个单独的动作,然后使用forkJoin返回关于它们是否都返回数据的我认为是可观察的。这似乎无法正常工作,因为3个组件之一通常在组件中返回undefined。
据我所知,select返回一个可观察值,并且一旦满足条件,filter / take(1)确保可观察值完成。这似乎工作正常,因为它在过滤器中多次击中,最终包含其中2个的数据。我不明白,如果其中一项不返回任何数据,为什么它将返回true。
有关如何使其正常工作的任何帮助?
getFromStoreOrAPI(): Observable<any> {
return forkJoin(
this.store.select(getFinancialFactors).pipe(filter(financialFactors => financialFactors && financialFactors.length > 0),take(1)),this.store.select(getFinancialData).pipe(filter(financialData => financialData && financialData.length > 0),this.store.select(getFranchiseData).pipe(filter(franchiseData => franchiseData && franchiseData.length > 0),take(1))
);
}
canActivateChild(route: ActivatedRouteSnapshot): Observable<boolean> | boolean {
let tranId = this.router.getCurrentNavigation().extras.state.tranId;
let modelId = this.outer.getCurrentNavigation().extras.state.modelId;
this.store.dispatch(ScorecardActions.loadFinancialData({tranId: tranId}));
this.store.dispatch(ScorecardActions.loadFranchiseData({tranId: tranId}));
this.store.dispatch(ScorecardActions.loadFinancialFactors({modelId: modelId}));
return this.getFromStoreOrAPI()
.pipe(
switchMap(() => of(true)),catchError(() => of(false))
);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。