使用RXJS forkjoin时传递参数的最佳方法

发布于 2025-01-31 19:46:18 字数 728 浏览 6 评论 0原文

因此,我的服务具有三个我需要执行的功能。

我使用forkjoin,因为我想在收到所有人的响应时采取进一步的行动! 其中一个需要接收一个参数。

getDevices(id: string): Observable<IEquipmentRow[]> {
    const url = `${apiUrl}/${id}`;
    return this.http.get<IGetDevicesResponse>(url)
      .pipe(
        map(res => {
          return res.data;
        })
      );
}

private regions$ = this.getRegions();
private devices$ = this.getDevices();


public equipmentPreparationData$ = forkJoin({
    regions: this.regions$,
    devices: this.devices$
});

实施该方法的最佳方法是什么?也许使用rxjs 主题/crapiorSubjectrxjs switchmap,我们可以在这里使用它吗? 我是新手rxjs,所以要温柔:)

So, I have a service that has three functions that I need to execute.

I use forkJoin because I want to take further action when a response has been received for all!
One of them needs to receive one parameter.

getDevices(id: string): Observable<IEquipmentRow[]> {
    const url = `${apiUrl}/${id}`;
    return this.http.get<IGetDevicesResponse>(url)
      .pipe(
        map(res => {
          return res.data;
        })
      );
}

private regions$ = this.getRegions();
private devices$ = this.getDevices();


public equipmentPreparationData$ = forkJoin({
    regions: this.regions$,
    devices: this.devices$
});

What is the best way to implement that? Maybe using RxJS Subject/BehaviorSubject?
What about RxJS switchMap, can we use it here?
I am new with RxJS, so be gentle :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七禾 2025-02-07 19:46:18

尝试:

// Change your response type
public equipmentPreparationData$(deviceID: string): Observable<any> {
 return forkJoin({
    regions: this.regions$,
    devices: this.getDevices(deviceID)
});


private getDevices(id: string): Observable<IEquipmentRow[]> {
    const url = `${apiUrl}/${id}`;
    return this.http.get<IGetDevicesResponse>(url)
      .pipe(
        map(res => {
          return res.data;
        })
      );
}

private regions$ = this.getRegions();

这样,您可以将函数与参数一起使用,并通过getDevices方法

Try with:

// Change your response type
public equipmentPreparationData$(deviceID: string): Observable<any> {
 return forkJoin({
    regions: this.regions$,
    devices: this.getDevices(deviceID)
});


private getDevices(id: string): Observable<IEquipmentRow[]> {
    const url = `${apiUrl}/${id}`;
    return this.http.get<IGetDevicesResponse>(url)
      .pipe(
        map(res => {
          return res.data;
        })
      );
}

private regions$ = this.getRegions();

In this way, you can use your function with the parameter and pass through getDevices method

柠檬色的秋千 2025-02-07 19:46:18

另外,可能的解决方案使用combineLatestmergemap用于传递ID参数:

  public woidSubject: BehaviorSubject<string> = new BehaviorSubject<string>("");
  public woidObservable: Observable<string> = this.woidSubject.asObservable();
  
  private devices$ = this.woidObservable.pipe(
    mergeMap(x => {
      debugger;
      return this.getDevices(x);
    })
  );

  public equipmentPreparationData$ = combineLatest([
    this.regions$,
    this.devices$
  ]).pipe(
    map(([regions, resourceTypes, devices]) => {
      return { regions: regions, devices: devices }
    })
  );

您可以从组件中更改ID参数:

this.service.woidSubject.next("3243");

Also, possible solution using combineLatest and mergeMap for passing id parameter:

  public woidSubject: BehaviorSubject<string> = new BehaviorSubject<string>("");
  public woidObservable: Observable<string> = this.woidSubject.asObservable();
  
  private devices$ = this.woidObservable.pipe(
    mergeMap(x => {
      debugger;
      return this.getDevices(x);
    })
  );

  public equipmentPreparationData$ = combineLatest([
    this.regions$,
    this.devices$
  ]).pipe(
    map(([regions, resourceTypes, devices]) => {
      return { regions: regions, devices: devices }
    })
  );

You can change id parameter from component like this:

this.service.woidSubject.next("3243");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文