angular - Angular observable 依赖于其他 observable
问题描述
我即将呈现一个我需要的页面
- 从路由器获取 id(使用 this.route.params)
- 使用步骤 1 中的 id 获取产品
- 使用第 2 步中产品中的 id 获取额外数据
现在我可以通过这样做来解决这个问题:
this.article$ = this.route.params.pipe(
map(params => params['articleId']),
switchMap(productId => this._articleService.getArticle(productId))
);
this.article$.subscribe(article => {
this.articleFormComponents$ = this._articleService.getArticleForm(article.form.id)
.pipe(
map(form => JSON.parse(form) as ArticleForm)
);
});
这对我来说看起来很乱......我应该如何改进这个?
解决方案
jonrsharpe 是正确的,但为了让您具体了解,您可以这样做:
this.article$ = this.route.params.pipe(
map(params => params['articleId']),
switchMap(productId => this._articleService.getArticle(productId))
);
this.articleFormComponent$ = this.article$.pipe(
switchMap(article => this._articleService.getArticleForm(article.form.id)),
map(form => JSON.parse(form) as ArticleForm),
);
确保订阅每个流以查看结果。
推荐阅读
- php - Php 响应标头在 https 上转换为小写
- javascript - 做出反应。如何在没有警告的情况下将 Grid 插入 TabPanel?
- c - epoll_wait :让它永远等待
- python - 我的 on_member_join 没有发送欢迎消息
- r - 在ggplot散点图中以特定方式覆盖点
- cmd - 新的 CMD 窗口打印:“■@”未被识别为内部或外部命令
- javascript - 带有 Graph API 的 Outlook Web 插件 - 将当前项目作为 Outlook 项目附件发送时不显示内联图像
- sed - 如何使用 sed 删除第一个数字之前的所有字母?
- java - 如何使用 Apache poi 3.6 和 Apache Poi-ooxml-3.15 jar 读取 pptx 文件的内容?
- node.js - mongodb,添加聚合还是带一个字段