angular - RxJs 和 Angular,我需要从路由中读取两个参数,并且我想使用可管道操作符
问题描述
给定一个 Angular 路由,我需要依次从路由器中读取两个参数。实际上我的代码有嵌套订阅,但我不太喜欢它们
this.route.parent.parent.params.subscribe(params => {
//read 'params'
this.route.parent.parent.data.subscribe(anotherParams => {
//read 'anotherParams' and do some thing with 'params'
})
})
我想使用 rxjs 运算符编写一次“订阅”,并在其中提供对两个参数的访问权限。
像这样的东西:
this.route.parent.parent.params.pipe( //some operators )
.subscribe ( params, anotherParams => {
'params' and 'anotherParams" readable inside that
})
解决方案
您可以使用mergeMap
、concatMap
或switchMap
为您订阅另一个内部可观察对象。
下面是一个使用 switchMap 的示例:
this.route.parent.parent.params.pipe(
switchMap(params => this.route.parent.parent.data.pipe(
map(otherParams => ({params , otherParams }))
))
).subscribe(({params, otherParams}) => {
// 'params' and 'anotherParams" readable inside that
});
推荐阅读
- objective-c - AdMob:找不到自动链接的框架“FBLPromises”
- javascript - 将状态从子组件提升到父组件
- apache-kafka - 卡夫卡红移连接器抛出 NullPointerException
- mongodb - mongoErrorContextSymbolError :与节点?
- java - 会计日期(ChronoLocalDate)到Java中的即时
- linux - Perforce,Linux - grep 查找已删除仓库文件中的内容
- python - 无法单击加载更多按钮,其中包含 href = javascript void using python
- python - 模型的测试和验证
- spring-boot - 如何使用 spring-cloud-stream-binder-kafka 和 RetryTemplate 启用有状态重试?
- python-3.x - 使用python在s3中自动生成文件名的最佳方法