【问题标题】:NGRX Entity: How to use it within template?NGRX 实体:如何在模板中使用它?
【发布时间】:2022-06-15 01:33:08
【问题描述】:

我遵循了有关如何使用 NGRX 实体实现 NGRX 存储的教程。

一切似乎都有效(据我所知,使用 dev-tools-extension)。但是,我不知道我应该/可以如何迭代模板中的结果。

模板:

<h3>MOVIES</h3>

  <ng-container *ngIf=\"movies$\">
      <table>
        <tbody>
        <tr *ngFor=\"let movie of (movies$ | async); let i = index\">
          <li>
            {{movie?.title}}
          </li>
        </tr>
        </tbody>
      </table>
  </ng-container>

组件:

@Component({
  selector: \'app-movies\',
  templateUrl: \'./movies.component.html\',
  styleUrls: [\'./movies.component.scss\']
})
export class MoviesComponent implements OnInit {

  movies$: Observable<Dictionary<Movie>>;

  constructor(private store: Store<MovieState>) {
    this.store.dispatch(loadMovies());
    this.movies$ = this.store.pipe(select(selectMovieEntities))
  }

  ngOnInit(): void {
  }

}

为了完整起见,reducer:

const {
  selectIds,
  selectEntities,
  selectAll,
  selectTotal
} = fromReducer.adapter.getSelectors();

export const getMovieState = createFeatureSelector<fromReducer.State>(fromReducer.moviesFeatureKey);

export const selectMovieEntities = createSelector(getMovieState, selectEntities);

错误消息:

我想知道我是否应该首先“映射”结果集或者这里的最佳实践是什么。

希望得到您的帮助!

  • 您能否添加您的状态界面或至少由 selectEntities 选择器选择的部分?

标签: angular ngrx ngrx-store ngrx-entity


【解决方案1】:

selectEntities 将实体状态作为字典返回(id 是键)。 如果您只想要实体(作为数组),请使用 selectAll 选择器。

export const selectMovieEntities = createSelector(getMovieState, selectAll);

【讨论】:

    猜你喜欢
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 2020-09-25
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多