簡體   English   中英

如何在 angular ngrx 中的效果中傳遞 id

[英]How to pass id in effects in angular ngrx

我正在選擇一個列表,並基於此我需要顯示選定的列表信息。

我對ngrx真的不熟悉,剛剛嘗試訂閱observable並獲取最后一個object:

View()方法:

constructor(private store: Store<IUserState>) {}

public user$: Observable<IUser[]>; -- declare once 

view(id) {
  this.store.dispatch(fromUserActions.loadUser({ id: id }));
  this.store.select(userQuery.getEntity).subscribe(value => {
    this.user$ = value[value.length - 1]; -- and assing the data as per values
    console.log(value[value.length - 1]);
  });
}

HTML:

<pre *ngIf="userLoaded$">
    {{user$ | json}}
</pre>  -- Removed async pipe

編輯:

view(id) {
    this.store.dispatch(fromUserActions.loadUser({ id: id }));
    this.store.select(userQuery.getEntity).subscribe(value => {
      this.user$ = new Observable<IUser>();
      this.user$ = value.filter(x=> x.id == id)[0];
      console.log(value.filter(x=> x.id == id)[0]);
    });
}

Demo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM