如何从商店中选择之前等待分发完成。 Ngrx相关问题

时间:2019-03-26 09:52:14

标签: node.js angular ngrx ngrx-effects ngrx-store-4.0

在从商店中进行选择之前,如何等待发送完成。谷歌搜索没有运气吗?在这种情况下,如何在从商店中进行选择之前先等待分发完成?

我的代码,感谢支持的帮助。

**team-list.component.ts**

 teamsState: Observable<{teams: Team[]}>;

  constructor(private store: Store<fromApp.AppState>) { }

  ngOnInit() {
    this.store.dispatch(new TeamActions.GetTeams({
      search: this.search,
      limit: this.limit,
      skip: this.skip,
      sortBy: this.sortBy
    }));
    this.teamsState = this.store.select('teams');
  }
**team-list.component.html**

<mat-expansion-panel
    *ngFor="let team of (teamsState | async).teams; let i = index">
    <mat-expansion-panel-header>
      <div class="container-fluid">
        <div class="row">
          <div class="col-md-1">{‌{ i+1 }}</div>
          <div class="col-md-1">
              <div class="post-image">
                <img [src]="imageUrl+team.imagePath" [alt]="team.name" style>
              </div>
          </div>
          <div class="col-md-10"> {‌{ team.name }} </div>
        </div>
      </div>
    </mat-expansion-panel-header>
effects
@Effect() // If you do not want to dispatch any actions, if need to modify store state then remove
    teamList = this.actions$.pipe(
        ofType(TeamActions.GET_TEAMS),
            map((action: TeamActions.GetTeams) => {
              return action.payload;
            }),
            switchMap((params: {search: string, limit: number, skip: number, sortBy: string}) => {
                return this.httpClient.get<Team[]>(
                  `${BACKEND_URL}?search=${params.search}&&limit=${params.limit}&&skip=${params.skip}&&sortBy=${params.sortBy}`);
            }),
            map((teams: Team[]) => {
                return {
                    type: TeamActions.SET_TEAMS,
                    payload: teams
                };
            }),
            catchError((err, caught) => {
              // console.log(err.error.errors);
              this.snackBarService.showSnackBar('Unable to Get Teams', true);
              return caught;
            })
        );

当前在第一次加载期间,当我从商店中选择项目时,调度动作尚未完成。当前为空。

3 个答案:

答案 0 :(得分:0)

不能,调度是一场大火,忘记了等不及了。

幸运的是,这是不需要的,因为this.store.select('teams')是可以观察到的。 这意味着,如果更改,则可观察对象将被发射一个新值,这将导致您的组件重新呈现。

如果列表为空,则可以检查您的状态是否实际上已更新,这可以通过@ngrx/store-devtools完成。如果状态已更新,但未在组件中显示,请确保不直接修改状态,而是要创建对数组的新引用。

答案 1 :(得分:0)

看看以下内容是否适合您。

UNNEST

以上是每个ngxs状态管理框架的内容。更多@ https://www.ngxs.io/advanced/actions-life-cycle#asynchronous-actions

答案 2 :(得分:0)

您可以做的就是在分派前清除选择器并过滤响应:

fromApp.GetStuff.release();
// the state is now empty
this.store.dispatch(fromApp.FetchMyStuff);
// any previous value has been forgotten. The next defined value is just what you want
this.stuff = this.store.select(fromApp.GetStuff).pipe(
    filter(stuff => !!stuff)
);

请参见https://ngrx.io/guide/store/selectors#resetting-memoized-selectors