ngrx - action start 和 store 更新时如何拦截?
问题描述
我正在使用 NGRX。我有很多操作,我想知道操作何时开始以及商店何时更新。
这个想法是无论执行什么操作,都有一个集中的方式来获取信息。我需要知道商店何时更新而不订阅所有选择器。
updateTitle ----> 标题已更新。
最好的,赫门德斯
解决方案
您必须明确定义操作
例如
import { createAction, props } from "@ngrx/store";
export enum HeaderActionTypes {
UpdateTitle = '[Title] Update Title',
UpdateTitleSuccess = '[Title] Update Title success'
}
export const UpdateTitle = createAction(HeaderActionTypes.UpdateTitle)
export const UpdateTitleSuccess = createAction(HeaderActionTypes.UpdateTitleSuccess, props<{ payload: string }>())
在 reducer 中,您可以捕获动作并更新状态
例如
import { createReducer, on } from "@ngrx/store";
export const initialState = {
... // Additional state properties can go here.
updatingTitle: false,
title: ''
}
export const reducer = createReducer(
initialState,
on(HeaderActionTypes.UpdateTitle, (state) => {
return {
...state,
updatingTitle: true
}
}),
on(HeaderActionTypes.UpdateTitleSuccess, (state, { payload }) => {
return {
...state,
updatingTitle: false,
title: payload
}
})
)
如果title
通过async
电话更新,您必须添加一个Effect
效果,查看文档。
Selectors,使用选择器读取状态并将其绑定到 UI。
推荐阅读
- python - 如何在python中可视化决策树?
- ios - 选项卡视图中的分段控件加载问题
- list - 何时选择容器/列表而不是切片
- java - 创建新项目时的弹簧缓存
- java - 如何检查属性文件路径是否设置正确
- terraform - CPU 和内存的 Terraform Openstack 输出参数
- javascript - 我想更改显示的 HTML 文本:#shadow root (user-agent)
- java - 大气websocket返回多个对象
- dart - RxDart,将列表的每个项目映射到来自永无止境的流的另一个对象
- sql - Magento SQL 在 multishops 中获取具有正确活动类别的产品 sku