嗨,我有一个现有的角1.6.x项目,在这个项目中,我做了一些类似的事情
var defer1 = $q.defer();
var defer2 = $q.defer();
$http.get(refTrustUrl).then(function (res) {
// some code here
defer1.resolve(true)
}, function () {
});
$http.get(candTrustUrl).then(function (res) {
// some code here
defer2.resolve(true)
}, function () {
});
$q.all([defer1.promise, defer2.promise]).then(function () {
// some code here
})现在我必须将这个项目迁移到Angular 4/5,在Observable中是否有与$q.all的功能相匹配的工作。注意:我读过关于Observable.forkJoin的文章,但没有看到我在哪里做了这样的事情:
Observable.forkJoin(
this.http.get(refTrustUrl, {responseType: 'text'}),
this.http.get(candTrustUrl, {responseType: 'text'})
).subscribe(
data=>{
console.log(data,1)
}
)但没起作用。请帮我..。我参考了这个http://www.metaltoad.com/blog/angular-2-http-observables来研究about__forkJoin
发布于 2018-05-02 08:39:00
您的代码应该是这样的(从您提供的链接)。
Observable.forkJoin(
this.http.get(refTrustUrl).map((res:Response) => res.json()),
this.http.get(candTrustUrl).map((res:Response) => res.json())
).subscribe(
data => {
this.refTrust = data[0]
this.candTrust = data[1]
},
err => console.error(err)
);如果有用,你能试试这个吗?
而且你也不需要订阅这样的内部流。
Observable.forkJoin(
this.http.get(refTrustUrl, {responseType: 'text'})
.subscribe(res => {当您完成所有转换时,您应该在最后订阅。
https://stackoverflow.com/questions/50130078
复制相似问题