NGRX实体:如何在模板中使用它?
我遵循了有关如何使用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="moviesquot;>
<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);
I'm wondering if I should "map" the result set first or what else is best practice here.
Hope for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
选择性
将实体状态返回为字典(ID是密钥)。如果您只想要实体(作为数组),请使用
selectall
selector。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.