我正在創建一個Observable來處理我的需要一個引數的資料。對于添加的新值,我使用了BehaviourSubject.
。 。heroes$。Observable<any> = this.heroService.heroes$.
constructor(private heroService: HeroService){
this.heroes$。 subscribe(x=> console。 log('ffffffffff',x)
)
}
//result up is empty
< li *ngFor="let hero of heroes$ | async">/span>
<a routerLink="/detail/{{hero.id}}"/span>>
<span class="badge">/span>{{hero. id}}</span>{{hero.contenttmsg}}。
</a>
<button class="delete" title="delete hero" >。
(click)="delete(hero)">x</button>/span>
</li>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
在服務中實作了一個簡單的邏輯來列出資料并添加一個新的資料。
heroes$ = merge(
this.allHeroes$。
this.heroCUDAction$.
.pipe(
tap(data => console. log('333333', data)), /data here displayed corectly。
scan((heroes, heroAction) => this. modifyHeroArray(heroes, heroAction), [] as any[] )。
);
private modifyHeroArray(heroes: any[] , value: Action<any> | any[]):any[] {
if (! (value instanceof Array) {
if (value.action == `add`) {
//將英雄添加到英雄陣列中。
return [...heroes, value.hero]。
}
} else {
return [...value]。
}
return heroes;
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
被呼叫的服務是一個Post請求
allHeroes$ = this.getSpecificMessage(
{
"id_conv"/span>:1,
"skipData": 0.
}
)
getSpecificMessage(req:any) 。Observable<any> {
return this.http.post<any> (
`http://localhost:3000/conversation/getSpecificMessage`, {
"from_user"/span>:1。
"to_user":2, "to_user":2
}, httpOptions
)
.pipe(
tap(data => console. log('data', data)) 。
catchError(this.handleError<any[]>('getHeroes'/span>, [] ))
)
}
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
解決了。
已經是一個不好的結果,從
scan((hero)
scan((heroes, heroAction) => this.modifyHeroArray(heroes, heroAction), [] as any[] ),
在我評論之后,能夠得到確切的結果。
uj5u.com熱心網友回復:
請注意你是如何轉換你的資料的。
添加管道和其他功能是可以的。 如果水龍頭在作業,訂閱也應該作業。 所以錯誤最終會出現在掃描上。
.pipe(
tap(data => console.log('333333', data)), //data here displayed corectly
scan((heroes, heroAction) => this.modifyHeroArray(heroes, heroAction), [] as any[] ),
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/319138.html
標籤:
