所以我有一個方法可以從服務器通過 id 獲取樹的父項
getParentById(id:string):return httpClient
我想遍歷這個方法并獲取從這個呼叫回傳的 parentId 并將其作為引數放回以獲取孩子,我只有第一個 parentId,所以我在第一次呼叫中傳遞它;
this.parentId ='123'
for(let i =0;i<arr.length;i ){
this.getNodeById(this.parentId).subscribe(res =>{
this.parentId = res.parentId;
// I am trying to append the parent id I have, so in the next iteration of th loop, the
parent id is updated
})
}
問題是第一個 parentId 值沒有改變,所有請求都使用相同的 parentId 發送,我該如何解決這個問題?
uj5u.com熱心網友回復:
問題很可能是您訂閱了此功能:
this.getNodeById(this.parentId)
xarr.length倍真的很快。
只有到那時你的值才會到達,所有的都是相同的 id。
uj5u.com熱心網友回復:
對您來說最好的解決方案是使用 EXPAND,一個用于遞回呼叫的 rxjs 運算子
import { from, EMPTY } from 'rxjs';
import { expand, tap} from 'rxjs/operators';
this.getNodeById(this.parentId).pipe(
tap(res => {
// do something on success
}),
expand((res) => res.parentId // call getNodeById if you have a parentId otherwise return EMPTY
? this.getNodeById(res.parentId)
: EMPTY;
)
).subscribe(console.log)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/335772.html
