我有以下代碼,其中 Obs2 依賴于 Obs1。但是,當我使用 combineLatest 時,我一直未定義 Obs1 的值。我怎樣才能讓它作業?我基本上只需要 Obs2$ 來過濾掉 Obs1$ 中存在的值。
this.Obs2$ = this.dataService.getObs2(this.id);
this.Obs1$ = this.dataService.getObs1(this.id)
.pipe(
map(item => {
return item.prop1
}),
tap( val => console.log('the tapped value: ', val)) // shows undefined
);
this.CombinedObs$ =
combineLatest(this.Obs1$,this.Obs2$)
.pipe(
map(([Obs1,Obs2]) => {
return Obs2
.filter(t => !Obs1.includes(t.prop1)) //obs1 keeps coming in as undefined
.map(item => {
return {
entityId: item.teamId,
entityName: item.teamName,
entityCategory: ''
}})
})
)
uj5u.com熱心網友回復:
我知道你在地圖管道上做錯了什么,如果這會回傳結果,請告訴我:
this.Obs2$ = this.dataService.getObs2(this.id);
this.Obs1$ = this.dataService.getObs1(this.id)
.pipe(
map(items => items.map(item => item.prop1)), // Issue was here
tap( val => console.log('the tapped value: ', val)) // It will not show undefinded now
);
this.CombinedObs$ =
combineLatest(this.Obs1$,this.Obs2$)
.pipe(
map(([Obs1,Obs2]) => {
return Obs2
.filter(t => !Obs1.includes(t.prop1)) //obs1 keeps coming in as undefined
.map(item => {
return {
entityId: item.teamId,
entityName: item.teamName,
entityCategory: ''
}}) // I'm sure you will get error over here so please review and handle that accordingly
})
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/399257.html
下一篇:在Angular中出錯:輸入'boolean|Observable<boolean>'不可分配給型別'boolean'
