q.all对于angular2可观察到的 [英] q.all for angular2 observables
本文介绍了q.all对于angular2可观察到的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有类似q.all之类的东西可以解决angular2中的所有http api请求?
is there something like q.all to resolve all http api requests in angular2?
在angular1中,我可以执行以下操作:
In angular1, I can do something like this:
var promises = [api.getA(),api.getB()];
$q.all(promises).then(function(response){
// response[0] --> A
// response[1] --> B
})
在angular2中,http模块返回Observable,
In angular2, the http module returns Observable,
api.getA().subscribe(A => {A})
api.getB().subscribe(B => {B})
但是我想一起解析A和B,然后做点事.
But I want to resolve A and B together, then do something.
推荐答案
您将需要.forkJoin
运算符
Observable.forkJoin([observable1,observable2])
.subscribe((response) => {
console.log(response[0], response[1]);
});
您可以导入Observable
;
import {Observable} from 'rxjs/Rx';
这篇关于q.all对于angular2可观察到的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文