Observable.forkJoin和数组参数 [英] Observable.forkJoin and array argument
问题描述
在观测量forkJoin文档,它说,ARGS可以是一个数组,但它并没有列出一个例子这样:
的https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/forkjoin.md
我曾尝试类似什么我上市(下同)但有错误想出了一个功能:
:3000 / angular2 / src目录/平台/浏览器/ browser_adapter.js:76例外:类型错误:Observable_1.Observable.forkJoin不是一个函数
我下面的函数的剪切版本:
processStuff(inputObject){ 让_self =这一点; 返回新观测(函数(观察员){ 让observableBatch = []; inputObject.forEach(功能(componentarray,键){
observableBatch.push(_self.http.get(键+'以.json')图((RES:响应)=> res.json()));
});
Observable.forkJoin(
observableBatch
//);
).subscribe(()=> {
observer.next();
observer.complete();
}); });
}
我的问题的根源是有关一个循环继续为这里问前结束:<一href=\"http://stackoverflow.com/questions/35566931/angular2-observable-how-to-wait-for-all-function-calls-in-a-loop-to-end-before\">Angular2观察到的 - 如何等待所有功能在一个循环中继续之前结束通话
但我还没有完全掌握正确的使用forkJoin的使用数组和正确的语法,这样做的。
我的帮助,你可以提供非常感激。
注意:例第三个函数返回一个可观察
的 thirdFunction(){ 让_self =这一点; 返回Observable.create((观察员)=&GT; { //返回新观测(函数(观察员){ ... observer.next(responseargs);
observer.complete();
});
}processStuff(inputObject){ 让_self =这一点。 让observableBatch = []; inputObject.forEach((componentarray,键)=&GT; {
observableBatch.push(_self.thirdFunction()图((RES:响应)=&GT; res.json()));
}); 返回Observable.forkJoin(observableBatch);
}别处(){
this.processStuff(inputObject)
。订阅()
}
您需要导入默认不加载运营商。这就是 EXCEPTION Observable.xxxx不是一个函数
通常意味着。您可以通过添加导入所有运营商完成 rxjs
来引导文件,例如:
进口'rxjs /接收
或导入特定的运营商,你的情况:
进口'rxjs /添加/可观测/ forkJoin`
你的code另一种意见/建议:尽量坚持一个语法。你是混合ES5,ES6,打字稿......虽然它正在它只会混淆你的长远目标。此外,如果你只是用观测量开始,尽量避免新观察,()
和使用创造符而不是;
processStuff(inputObject){
让observableBatch = []; inputObject.forEach((componentarray,键)=&GT; {
observableBatch.push(this.http.get(键+'以.json')图((RES:响应)=&GT; res.json()));
}); 返回Observable.forkJoin(observableBatch);
}别处(){
this.processStuff(inputObject)
。订阅()
}
最后,引用正确的文件 - Angular2使用 RxJS V5 以及链接,您提供的是RxJS V4 。文档仍然是不完整的V5的,但你可以在很多的源文件找到说明。
In the Observables forkJoin documentation, it says that args can be an array but it doesn't list an example doing so:
https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/forkjoin.md
I have tried a function similar to what I listed (below) but came up with an error:
:3000/angular2/src/platform/browser/browser_adapter.js:76
EXCEPTION: TypeError: Observable_1.Observable.forkJoin is not a function
A sheared version of my function below:
processStuff( inputObject ) {
let _self = this;
return new Observable(function(observer) {
let observableBatch = [];
inputObject.forEach( function( componentarray, key ) {
observableBatch.push( _self.http.get( key + '.json').map((res: Response) => res.json()) );
});
Observable.forkJoin(
observableBatch
// );
).subscribe(() => {
observer.next();
observer.complete();
});
});
}
The root of my question is related to a loop to end before proceeding as asked here: Angular2 Observable - how to wait for all function calls in a loop to end before proceeding?
But I haven't fully mastered the correct use of forkJoin with an array and the right syntax to do so.
I am very grateful for help you could offer.
NOTE: EXAMPLE OF THIRD FUNCTION THAT RETURNS AN OBSERVABLE
thirdFunction() {
let _self = this;
return Observable.create((observer) => {
// return new Observable(function(observer) {
...
observer.next( responseargs );
observer.complete();
});
}
processStuff( inputObject ) {
let _self = this.
let observableBatch = [];
inputObject.forEach(( componentarray, key ) => {
observableBatch.push( _self.thirdFunction().map((res: Response) => res.json()) );
});
return Observable.forkJoin(observableBatch);
}
elsewhere() {
this.processStuff( inputObject )
.subscribe()
}
You need to import operators that are not loaded by default. That's what EXCEPTION Observable.xxxx is not a function
usually means. You can either import all operators by adding complete rxjs
to your bootstrap, for example:
import 'rxjs/Rx'
or by importing specific operators, in your case:
import 'rxjs/add/observable/forkJoin`
Another observation/suggestion about your code: try to stick with one syntax. You are mixing es5, es6, typescript... and while it is working it will only confuse you in the long run. Also, if you're just starting with Observables, try to avoid new Observable()
and use creation operators instead;
processStuff( inputObject ) {
let observableBatch = [];
inputObject.forEach(( componentarray, key ) => {
observableBatch.push( this.http.get( key + '.json').map((res: Response) => res.json()) );
});
return Observable.forkJoin(observableBatch);
}
elsewhere() {
this.processStuff( inputObject )
.subscribe()
}
Finally, refer to the correct documentation - Angular2 uses RxJS v5 and link you provided is for RxJS v4. Docs are still incomplete for v5, but you can find descriptions in many of the source files.
这篇关于Observable.forkJoin和数组参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!