我很难弄清楚如何用一个观察点的结果来更新另一个观察点的结果:

this._http.get<JSON>(url, { params })
.pipe(map((res: JSON) => this.getObjectFromJson(res)))
.subscribe(myObject => {
    this.getValueToUpdate(myObject).subscribe(newValue => {
        myObject.some.value = newValue;
    });
});
// return Observable<myObect>;

Rxjs运算符的正确顺序是什么,才能在没有这subscribe的情况下实现这一点,并能够返回生成的可观测对象以供以后使用?

推荐答案

您要找的是switchMap:

this._http.get<JSON>(url, { params }).pipe(
  map((res: JSON) => this.getObjectFromJson(res)),
  switchMap(myObject => {
    return this.getValueToUpdate(myObject).pipe(
      // probably here you need also a catchError(err => ...),
      map(newValue => {
        myObject.some.value = newValue;
        return myObject;
      })
    );
  })
).subscribe(myObject => {
  // here myObject is updated with newValue
});

It allows to invoke another async operation and switch (i.e. observe) on that.
The inner pipe, using the map operator, allows to combine the result of the nested async operation with the previous result.

Angular相关问答推荐

使用路由进行测试.为每次测试导航堆栈

RFDC.CommonJS或AMD依赖项可能会导致优化救助ANGURAL PROCEMENT with ngx-charts lib

Angular前端调用ALB身份验证后的后端并重定向

Angular Signal只指一种类型,但这里用作值.ts(2693)''

当try 使用Angular和PrimeNg手动聚焦输入时,我做错了什么?

仅在从forkJoin获得数据后订阅主题

[已解决]如何在模块ngDoBootstrap中测试Angular 自定义元素和做覆盖

Angular 如何渲染ngFor?

以Angular 动态添加和删除组件形状元素

NGX- colored颜色 Select 器安装出错

Angular 信号 - 使用 mutate() 与使用 forEach() react 性

如何在 Angular14 中创建带有验证的自定义输入组件?

Angular6:RxJS switchMap 运算符未按预期工作

Angular 项目构建仅在 GitHub Actions 上失败

如何在 ngFor 循环中创建变量?

如何在 Angular 6 中使用 mouseover 和 mouseout

Angular 2 中的 $implicit 是什么?

交替行 colored颜色 angular material表

安装特定版本的 ng cli

Angular 2 可用的 yeoman 生成器