angular - Ngrx 存储:使用额外数据对特定切片更改执行选择处理程序
问题描述
我有一个商店状态:
export interface DocumentState {
document: Document;
selectedPage: number;
}
我想打电话DoSomethingOnlyOnDocumentChange(doc: Document)
和DoSomethingOnlyOnPageChange(Document: doc, page: number)
。这个想法不是在其他状态更改时执行处理程序。我已订阅状态更改,如下所示:
this.store$.select(appState => appState.documentState.document).subscribe(this.DoSomethingOnlyOnDocumentChange);
this.store$.select(appState => appState.documentState.selectedPage).subscribe(this.DoSomethingOnlyOnPageChange);
如何在页面更改状态处理程序中访问文档?
解决方案
这就是存在的理由ngrx selectors
。
首先为您需要的状态切片创建一个选择器:
export const selectDocument = createFeatureSelector<AppState,
DocumentState>('document');
export const getDocument = createSelector(
selectDocument,
(state: DocumentState) => state.document
);
然后在您的组件中使用它:
this.store$.select(getDocument).subscribe(this.DoSomethingOnlyOnDocumentChange);
这样,只有对文档切片所做的更改才会被处理DoSomethingOnlyOnDocumentChange
参考:ngrx 文档
推荐阅读
- javascript - 如何将 Object {} 转换为一维数组?
- html - 图像未显示在 S3 URL 的 Thymeleaf 图像 src 中
- heroku - Heroku GUI 在多种情况下显示“未知错误”
- javascript - rollup.js - 如何加载捆绑的 CSS?
- mysql - Amazon VPC:将默认 VPC 服务器连接到非默认 VPC 服务器
- python - 在规则中的函数中使用通配符
- spring-kafka - 如何访问 SCDF 流中的源本机标头?
- pandas - 与首先选择列相比,对整个数据框进行分组,然后是过滤列是否占用更多内存?
- intellij-idea - IntelliJ - 前一天的提示
- rtmp - RTMP客户端什么时候发起deletestream()的操作