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

How to wait for dispatch to be completed before selecting from a store. Ngrx related issue

在我从商店 select 之前如何等待发货完成。谷歌搜索没有运气?在这种情况下,如何在 select 从商店发货之前先等待发货完成?

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

**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;
            })
        );

目前在第一次加载期间,调度操作尚未完成,当我 select 商品来自商店时。目前是空的。

不可以,一派火烧,忘了等不及了。

幸运的是,这不是必需的,因为 this.store.select('teams') 是可观察的。 这意味着如果它发生变化,observable 将发出一个新值,这将导致您的组件重新渲染。

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

看看下面的方法是否适合你。

this.store.dispatch(new TeamActions.GetTeams({
      search: this.search,
      limit: this.limit,
      skip: this.skip,
      sortBy: this.sortBy
    }))
    .subscribe(() => {
        this.teamsState = this.store.select('teams');
    });

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

您可以做的是在发送之前清除选择器并过滤响应:

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