combineLatest可以作为输入的参数的最大数量是 6。
更具体地说,它采用一个最多可包含 6 个元素的数组作为输入。这样可以自动推断类型。
因此,如果在下面的代码中添加我会this.someMethodReturningObservable7()破坏它:
someMethod(...) {
return combineLatest([
this.someMethodReturningObservable1(),
this.someMethodReturningObservable2(),
this.someMethodReturningObservable3(),
this.someMethodReturningObservable4(),
this.someMethodReturningObservable5(),
this.someMethodReturningObservable6(),
this.someMethodReturningObservable7(), // this causes problems
]).pipe(
map(([arg1, arg2, arg3, arg4, arg5, arg6, arg7]) => ({
arg1,
arg2,
arg3,
arg4,
arg5,
arg6,
arg7,
})),
// do more stuff
);
}
Run Code Online (Sandbox Code Playgroud)
只要我将其保持在 6 个参数,它就可以正常工作。超过6个就不行了。
我怎样才能将其分解为几个,combineLatest每个最多有 6 个参数?
小智 10
您可以将两个 mergeLatests 合并为一个,如下所示:
import { of, combineLatest, interval } from "rxjs";
import { map, take } from "rxjs/operators";
const num0$ = of(0 as const);
const num1$ = of(1 as const);
const num2$ = of(2 as const);
const num3$ = of(3 as const);
const num4$ = of(4 as const);
const num5$ = of(5 as const);
const num6$ = of(6 as const);
const num7$ = of(7 as const);
const num8$ = of(8 as const);
const num9$ = interval(1000).pipe(take(3), map(() => 9 as const));
const numsPartOne$ = combineLatest(num0$, num1$, num2$, num3$, num4$);
const numsPartTwo$ = combineLatest(num5$, num6$, num7$, num8$, num9$);
const numCombined$ = combineLatest([numsPartOne$, numsPartTwo$]).pipe(
// use [[..] [..]] to spread both arrays
map(([[num0, num1, num2, num3, num4], [num5, num6, num7, num8, num9]]) => {
// num0 has type 0, etc.
return {
num0,
num1,
num2,
num3,
num4,
num5,
num6,
num7,
num8,
num9
};
})
);
numCombined$.subscribe(mappedNums => console.log(mappedNums));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1726 次 |
| 最近记录: |