如何在 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]) => {
// ...
});
}
在第一個代碼片段中,訂閱投影中的每個專案都是正確的型別。如果我用 just 替換投影response,它的型別將是[firstResponseType, secondResponseType].
在第二個代碼片段中,訂閱投影中的每個專案都是型別somethingProjectionType | firstResponseType | secondResponseType。如果我用 just 替換投影response,它的型別將是(somethingProjectionType | firstResponseType | secondResponseType)[].
如何傳遞somethingProjection給下一個訂閱,以便陣列描述中的每個專案都是正確的型別?
uj5u.com熱心網友回復:
您必須回傳一個具有所需屬性的物件,而不是從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 }) => {
// ...
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/431909.html
