接下来如何通过concatMap投影进行订阅呢?
How to pass concatMap projection to 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
});
}
我找到了使用 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]) => {
// ...
});
}
在第一个代码片段中,订阅投影中的每个项目都是正确的类型。如果我只用 response
替换投影,它的类型将是 [firstResponseType, secondResponseType]
.
在第二个代码片段中,订阅投影中的每个项目都是 somethingProjectionType | firstResponseType | secondResponseType
类型。如果我只用 response
替换投影,它的类型将是 (somethingProjectionType | firstResponseType | secondResponseType)[]
.
如何将 somethingProjection
传递给下一个订阅,以便数组描述中的每一项都是正确的类型?
您必须 return 具有所需属性的对象,而不是 return 来自 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 }) => {
// ...
});
}
如何在 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
});
}
我找到了使用 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]) => {
// ...
});
}
在第一个代码片段中,订阅投影中的每个项目都是正确的类型。如果我只用 response
替换投影,它的类型将是 [firstResponseType, secondResponseType]
.
在第二个代码片段中,订阅投影中的每个项目都是 somethingProjectionType | firstResponseType | secondResponseType
类型。如果我只用 response
替换投影,它的类型将是 (somethingProjectionType | firstResponseType | secondResponseType)[]
.
如何将 somethingProjection
传递给下一个订阅,以便数组描述中的每一项都是正确的类型?
您必须 return 具有所需属性的对象,而不是 return 来自 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 }) => {
// ...
});
}