简体   繁体   English

NGRX 实体:如何在模板中使用它?

[英]NGRX Entity: How to use it within template?

I followed a tutorial on how to implement an NGRX store with an NGRX entity.我遵循了有关如何使用 NGRX 实体实现 NGRX 存储的教程。

Everything seems to work (as far as I can tell using the dev-tools-extension).一切似乎都有效(据我所知,使用 dev-tools-extension)。 However, I don't know how I should/can iterate over the result in the template.但是,我不知道我应该/可以如何迭代模板中的结果。

The template:模板:

<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>

The component:组件:

@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 {
  }

}

And for completeness, the reducer:为了完整起见,reducer:

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

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

export const selectMovieEntities = createSelector(getMovieState, selectEntities);

The Error-Message:错误消息: 在此处输入图像描述

I'm wondering if I should "map" the result set first or what else is best practice here.我想知道我是否应该首先“映射”结果集或者这里的最佳实践是什么。

Hope for your help!希望得到您的帮助!

selectEntities returns the entities state as a dictionary (id is the key). selectEntities将实体状态作为字典返回(id 是键)。 If you just want the entities (as an array), use the selectAll selector.如果您只想要实体(作为数组),请使用selectAll选择器。

export const selectMovieEntities = createSelector(getMovieState, selectAll);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM