forkjoin在Angular10替代方案中已弃用
下面是我的代码:
async getBranchDetails() ----component method
{
let banks = this.bankDataService.getBanks();
let branchTypes = this.branchDataService.getBranchTypes();
forkJoin([banks,branchTypes]).subscribe(results => {
this.setFormBankData(results[0]);
this.setFormBranchTypeData(results[1]);
});
}
- - 服务
async getBanks(): Promise<IBankResponse[]> {
return await this.httpClient.get<Result<IBankResponse[]>>(baseUrl + '/Bank/GetBanks')
.pipe(map( res => res.data)).toPromise();
}
Fork join 显示已弃用。是否有任何其他用途async/ await。谢谢。
编辑:我不知道它是否正确,但使用了 asyn/await ..我的最终代码如下
async getBranchDetails()
{
let banks = await this.bankDataService.getBanks();
let branchTypes= await this.branchDataService.getBranchTypes();
this.setFormBankData(banks);
this.setFormBranchTypeData(branchTypes);
}
回答
您正在混合 Observables 和 Promises。要么使用 Observables 和 RxJS,要么坚持使用 Promises。
Observables 方法(推荐):
getBanks(): Observable<IBankResponse[]> {
return this.httpClient.get(baseUrl + '/Bank/GetBanks')
.pipe(map( res => res.data));
}
let banks = this.bankDataService.getBanks();
let branchTypes = this.branchDataService.getBranchTypes();
forkJoin([banks,branchTypes]).subscribe(results => {
this.setFormBankData(results[0]);
this.setFormBranchTypeData(results[1]);
});
承诺方法:
getBanks(): Promise<IBankResponse[]> {
return this.httpClient.get<Result<IBankResponse[]>>(baseUrl + '/Bank/GetBanks')
.pipe(map( res => res.data)).toPromise();
}
使用Promise 时,您可以使用Promise.all来等待所有请求完成:
let banks = this.bankDataService.getBanks();
let branchTypes = this.branchDataService.getBranchTypes();
Promise.all([banks,branchTypes]).then(results => {
this.setFormBankData(results[0]);
this.setFormBranchTypeData(results[1]);
});