angular - 如何将同一个可观察对象与多个映射一起使用?

标签 angular rxjs angular-httpclient subject-observer

我的代码有一个 Subject,当添加新值时,它会触发一个返回 Observable 的 HTTP 请求。

我想以两种不同的方式(使用相同的数据)处理这些数据,并使用存储在 grouptimes 中的结果 Observables作为使用 async 管道放入 ngFor 的值。

虽然这确实有效,但 HTTP 请求会被发送多次 - 我只想为每个订阅发送一次。

下面是一个最小的例子。

import { Component, OnInit } from "@angular/core";
import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/Subject";

import { ExampleService } from "../example.service";

import "rxjs/add/operator/switchMap";

@Component({
  templateUrl: "./example.component.html",
  styleUrls: ["./example.component.scss"]
})
export class ExampleComponent implements OnInit {

  constructor(
    private exampleService: ExampleService
  ) { }

  ngOnInit() {

    var selections = new Subject<string>();

    var appointments = selections
      // exampleService.getData returns an HTTP observable.
      .switchMap(date => this.exampleService.getData(date));

    var group = appointments
      .map(data => this.process(data));

    var times = appointments
      .map(data => this.calculateTimes(data));

    // Calling subscribe each time sends the HTTP request multiple
    // times - I only want it to be send once for both of them: they
    // can share the data!!
    group.subscribe();
    times.subscribe();

    // selections.next(someData) is called periodically from some
    // omitted code.
  }

  processExample(data: string[]) {
    /*
     * Some processing code.
    */

    return data;
  }

  calculateTimes(data: string[]) {
    /*
     * Some processing code.
    */

    return data;
  }
}

实现此目标的最佳方法是什么?

最佳答案

您可以使用share 运算符来存档您要查找的内容:

import 'rxjs/add/operator/share';

ngOnInit() {

  var selections = new Subject<string>();

  var appointments = selections
    // exampleService.getData returns an HTTP observable.
    .switchMap(date => this.exampleService.getData(date))
    .share();

  var group = appointments
    .map(data => this.process(data));

  var times = appointments
    .map(data => this.calculateTimes(data));

  // Calling subscribe each time sends the HTTP request multiple
  // times - I only want it to be send once for both of them: they
  // can share the data!!
  group.subscribe();
  times.subscribe();

  // selections.next(someData) is called periodically from some
  // omitted code.
}

基本上,不同的观察者共享相同的source

有关运营商的更多信息 here .

关于angular - 如何将同一个可观察对象与多个映射一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46964453/

相关文章:

angular - 有什么方法可以改变 Angular 5 HttpInterceptor 中的响应吗?

javascript - 在 GET 请求中使用激活的路由参数变量返回 null

Angular 6 : TypeError: Cannot read property 'scrollIntoView' of null

rxjs, forkJoin(array).mergeMap(...), 内部 mergeMap 永远不会被调用

javascript - 从服务返回订阅 - 但如何在服务中使用异步结果?

angular - NgRx - 在路由器转换期间正在分派(dispatch)多个操作

javascript - 为什么 setTimeout() 使我的应用程序滞后,而 Rxjs timer().subscribe(...) 却没有?

angular - Angular 2+的 masonry 替代品

Angular 6,SonarQube : src folder not ignored while checking package. json

angular - 如何将参数传递给自定义 Angular webpack 构建?