问题
I make this code for test forkJoin but it does not work! Can you check what is the problem?
const observables = [];
observables.push(new Observable(subscriber => subscriber.next('Hello')));
observables.push(new Observable(subscriber => subscriber.next(' ')));
observables.push(new Observable(subscriber => subscriber.next('World') ));
observables.push(new Observable(subscriber => subscriber.next('!')));
forkJoin(observables).subscribe(word => console.log(word.join('')));
回答1:
Try this
const observables = [];
observables.push(Observable.of('Hello'));
observables.push(Observable.of(' '));
observables.push(Observable.of('World'));
observables.push(Observable.of('!'));
Observable.forkJoin(observables).subscribe(word => console.log(word.join('')));
回答2:
Is important complete the observable for forkJoin work, like the example!
const observables = [];
observables.push(new Observable(subscriber => {
subscriber.next('Hello');
subscriber.complete();
}));
observables.push(new Observable(subscriber => {
subscriber.next(' ');
subscriber.complete();
}));
observables.push(new Observable(subscriber => {
subscriber.next('World');
subscriber.complete();
}));
observables.push(new Observable(subscriber => {
subscriber.next('!');
subscriber.complete();
}));
forkJoin(observables).subscribe(word => console.log(word.join('')));
来源:https://stackoverflow.com/questions/48300456/forkjoin-issue-on-angular