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

发布于 2025-02-07 11:34:57 字数 1638 浏览 2 评论 0原文

我遵循了有关如何使用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 {
  }

}

为了完整性,还原器:

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

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

export const selectMovieEntities = createSelector(getMovieState, selectEntities);

错误消息:

我想知道我是否应该“映射”结果集,还是在这里还有什么最佳练习。

希望您的帮助!

I followed a tutorial on how to implement an NGRX store with an NGRX entity.

Everything seems to work (as far as I can tell using the 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
quot;>
      <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:

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:
enter image description here

I'm wondering if I should "map" the result set first or what else is best practice here.

Hope for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔纱相望 2025-02-14 11:34:57

选择性将实体状态返回为字典(ID是密钥)。
如果您只想要实体(作为数组),请使用selectall selector。

export const selectMovieEntities = createSelector(getMovieState, selectAll);

selectEntities returns the entities state as a dictionary (id is the key).
If you just want the entities (as an array), use the selectAll selector.

export const selectMovieEntities = createSelector(getMovieState, selectAll);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文