将最新数据与可观察数组结合起来
如何将 combineLatest
与两个或多个可观察数组一起使用?
const array1 = [of(1), of(2)];
const array2 = [of(3), of(4)];
combineLatest(array1, array2)
.subscribe(([array1, array2]) => {
console.log(
`array1[0]: ${array1[0]}
array1[1]: ${array1[1]}
array2[0]: ${array2[0]}
array2[1]: ${array2[1]}`
);
});
我有一个堆栈闪电战: https://stackblitz.com/edit/typescript-cnzvlo? file=index.ts&devtoolsheight=100
How would one go about using combineLatest
with two or more arrays of observables?
const array1 = [of(1), of(2)];
const array2 = [of(3), of(4)];
combineLatest(array1, array2)
.subscribe(([array1, array2]) => {
console.log(
`array1[0]: ${array1[0]}
array1[1]: ${array1[1]}
array2[0]: ${array2[0]}
array2[1]: ${array2[1]}`
);
});
I have a stackblitz:
https://stackblitz.com/edit/typescript-cnzvlo?file=index.ts&devtoolsheight=100
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
combineLatest
函数需要一个Observable
数组,而不是Observable
数组的数组。因此,您需要做的是:
Observable
的内部Observable
数组,将每个Observable
数组转换为Observable
其中之一使用concat
函数。您可以执行如下操作:
这是 StackBlitz 的工作版本:
https://stackblitz.com/edit/typescript-3imea6?file=index.html ts
The
combineLatest
function expects an array ofObservable
(s), not an array of array ofObservable
.So what do you have to do is:
Observable
, to anObservable
, by combining the innerObservable
(s) of each one of them usingconcat
function.toArray
operator.You can do something like the following:
And here is the working version of your StackBlitz:
https://stackblitz.com/edit/typescript-3imea6?file=index.ts