简体   繁体   English

如何在angularfire2中使用Observable.Zip?

[英]How to use Observable.Zip with angularfire2?

I'm using Angular 5 and angularfire2 to work with Firebase. 我正在使用Angular 5和angularfire2来使用Firebase。 I have an array of strings which represent paths into firebase. 我有一个字符串数组,表示进入firebase的路径 What I'm currently doing is this: 我目前正在做的是:

const pathList: string[] = ['path1', 'path2', 'path3'];
const observableList     = [];
pathList.map((location: string[]) => {
    observableList.push(this.db.object(`path_to_something/${location}).valueChanges());
});

const combined = zip(observableList);
combined.subscribe((values: any[]) => {
    console.log('DATA FROM ZIP', values);
}, err => {
    console.log('DATA FROM ZIP ERROR', err);
});

That's what I've been trying but, the Observables doesn't seem to trigger. 这就是我一直在尝试但是,Observables似乎没有触发。 Any result on the console is displayed. 显示控制台上的任何结果。 Any idea on how this could work. 任何关于如何工作的想法。

I tried with forkJoin and nothing as well. 我尝试使用forkJoin,也没有尝试过。 But I tried with combineList and it works. 但我尝试使用combineList,它的工作原理。 The issue with that is, I have to use zip 问题是,我必须使用zip

If the same Rx chain worked with combineLatest it should work with zip as well. 如果相同的Rx链与combineLatest一起使用,它也应该与zip一起使用。 If it doesn't emit anything try unwrapping the array of Observables: 如果它没有发出任何东西,请尝试展开Observables数组:

const combined = zip(...observableList);

You can try it with the following demo and see that if you pass the array as is it won't emit anything: 您可以使用以下演示进行尝试,并查看如果您按原样传递数组,它将不会发出任何内容:

http://jsbin.com/davadol/3/edit?js,console http://jsbin.com/davadol/3/edit?js,console

const a = Observable.of(1);
const b = Observable.of(2);
const c = Observable.of(3);

Observable.zip([a, b, c]).subscribe(console.log);

But if you unwrap the array it works: 但是,如果你解开它的数组:

Observable.zip(...[a, b, c]).subscribe(console.log);

To be honest I'm surprised it doesn't work with the array because by looking at the typings I think it should, see https://github.com/ReactiveX/rxjs/blob/5.5.6/src/operators/zip.ts#L26 . 说实话,我很惊讶它不适用于数组,因为通过查看我认为它应该的类型,请参阅https://github.com/ReactiveX/rxjs/blob/5.5.6/src/operators/zip .ts#L26 This should be the same use-case like with combineLatest and forkJoin where it works. 这应该与使用combineLatestforkJoin用例相同。 To my surprised I recently found out that it doesn't work with Observable.concat either. 令我惊讶的是,我最近发现它也不适用于Observable.concat

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

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