gpt4 book ai didi

javascript - 永远不会调用 Observable.forkJoin 的订阅

转载 作者:行者123 更新时间:2023-11-30 11:54:11 25 4
gpt4 key购买 nike

我需要等待 2 个 AJAX 请求的结果才能对其进行一些计算。

我找到了似乎可以完成这项工作的 Observable.forkJoin 方法。所以在我的组件中我添加了:

import { Observable } from 'rxjs/Rx';

// ...

ngOnInit() {
Observable.forkJoin(
this.instrumentService.current,
this.tuningService.tunings
).subscribe(
data => {
console.log(data);
}
);
}

AJAX 请求已发送,但我从未传入 subscribe 回调。

如果我尝试分别执行每个请求,它工作正常:

 this.instrumentService.current.subscribe(instrument => {
console.log(instrument);
this.instrument = instrument;
});

this.tuningService.tunings.subscribe(tunings => {
console.log(tunings);
this.tunings = tunings;
});

在数据服务(instrumentServicetuningService)中,Observable是这样声明的(可能是bug的来源,别不知道):

private _tunings: BehaviorSubject<Tuning[]> = new BehaviorSubject([]);

/**
* Class constructor.
*
* @param {ApiService} apiService
*/
constructor (private apiService: ApiService) {
this.getAll().subscribe(tunings => this._tunings.next(tunings));
}

public get tunings() {
return this._tunings.asObservable();
}

public getAll(): Observable<Tuning[]> {
return this.apiService.call(this.url);
}

apiService负责调用内置的angular http服务。

最佳答案

据我所知 forkJoin 目前有一个错误,如果 Observable 之一返回 null 那么 forkJoin 订阅永远不会被调用。您可以尝试使用 .zip 吗?

关于javascript - 永远不会调用 Observable.forkJoin 的订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38551110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com