Elk*_*kin 1 typescript ngrx angular
我正在学习NgRx,但我不明白为什么实体选择器触发动作.我一直在寻找原因,但我没有找到任何东西.也许我错过了一些NgRx/Entity的基础知识.
这是我的演示代码:
selectors.ts
export const selectHeroState: MemoizedSelector<object, HeroesState> = createFeatureSelector<HeroesState>('heros');
export const selectHeroes: (state: object) => Hero[] = heroAdapter.getSelectors(selectHeroState).selectAll;
Run Code Online (Sandbox Code Playgroud)
component.ts
ngOnInit() {
//this.store$.dispatch(new GetAll()); I thought this line of code was nescessary to fetch all data from store...
this.heroes$ = this.store$.select(selectHeroes); //but i just need this one as it triggers the Get All action
}
Run Code Online (Sandbox Code Playgroud)
我希望能很好地解释我的问题.提前致谢.
选择器不会触发操作.必须有其他东西引发行动?
例如,在我的ngOnInit中我有这个:
this.store.dispatch(new productActions.Load());
Run Code Online (Sandbox Code Playgroud)
这就是加载我的数据的原因.然后我可以这样:
this.products$ = this.store.pipe(select(fromProduct.getProducts)) as Observable<Product[]>;
Run Code Online (Sandbox Code Playgroud)
哪个选择器可以获取用于UI的数据.
只是为了确认,我尝试dispatch在上面评论我,但它停止了工作(它没有返回任何数据).
所以我的猜测是应用程序中的其他地方已经加载了你的数据?
| 归档时间: |
|
| 查看次数: |
136 次 |
| 最近记录: |