简体   繁体   English

接下来如何通过concatMap投影进行订阅呢?

[英]How to pass concatMap projection to subscribe next?

How can I use the concatMap projection in the subscribe.next()?如何在 subscribe.next() 中使用 concatMap 投影?

    private onSomethingChange(): Subscription {
        // somethingChanged is a Subject
        return this.somethingChanged.pipe(
            concatMap(somethingProjection =>
                combineLatest([
                    this.firstObservable(somethingProjection),
                    this.secondObservable()
                ])
            )).subscribe(([firstResponse, secondResponse]) => {
                // I need to use somethingProjection here too
        });
    }

I found suggestions to use RxJS map , but I haven't found a way to use it correctly:我找到了使用 RxJS map的建议,但我还没有找到正确使用它的方法:

    private onSomethingChange(): Subscription {
        // somethingChanged is a Subject
        return this.somethingChanged.pipe(
           concatMap(somethingProjection =>
               combineLatest([
                   this.firstObservable(somethingProjection),
                   this.secondObservable()
               ]).pipe(map(([firstResponse, secondResponse]) => [somethingProjection, firstResponse, secondResponse])
           )).subscribe(([somethingProjection, firstResponse, secondResponse]) => {
               // ...
        });
   }

In the first code snippet, each item in the subscribe projection is of the correct type.在第一个代码片段中,订阅投影中的每个项目都是正确的类型。 If I replace the projection with just response , its type would be [firstResponseType, secondResponseType] .如果我只用response替换投影,它的类型将是[firstResponseType, secondResponseType]
In the second code snippet, each item in the subscribe projection is of type somethingProjectionType | firstResponseType | secondResponseType在第二个代码片段中,订阅投影中的每个项目都是类型somethingProjectionType | firstResponseType | secondResponseType somethingProjectionType | firstResponseType | secondResponseType somethingProjectionType | firstResponseType | secondResponseType . somethingProjectionType | firstResponseType | secondResponseType If I replace the projection with just response , its type would be (somethingProjectionType | firstResponseType | secondResponseType)[] .如果我只用response替换投影,它的类型将是(somethingProjectionType | firstResponseType | secondResponseType)[]
How to pass somethingProjection to the subcribe next so that each item in the array descrtion is of the correct type?如何将somethingProjection传递给下一个订阅,以便数组描述中的每个项目都是正确的类型?

Instead of returning an array from the map operator, you have to return an object with the required properties.您必须返回具有所需属性的 object,而不是从map运算符返回数组。

You can try something like the following:您可以尝试以下操作:

private onSomethingChange(): Subscription {
  // somethingChanged is a Subject
  return this.somethingChanged
    .pipe(
      concatMap((somethingProjection) =>
        combineLatest([
          this.firstObservable(somethingProjection),
          this.secondObservable(),
        ]).pipe(
          map(([firstResponse, secondResponse]) => ({
            somethingProjection,
            firstResponse,
            secondResponse,
          }))
        )
      )
    )
    .subscribe(({ somethingProjection, firstResponse, secondResponse }) => {
      // ...
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM