Angular Ngrx存储:对具有额外数据的特定片更改执行选择处理程序
我的门店状态为:Angular Ngrx存储:对具有额外数据的特定片更改执行选择处理程序,angular,ngrx-store,Angular,Ngrx Store,我的门店状态为: export interface DocumentState { document: Document; selectedPage: number; } 我想调用dosomethingonloyondocumentchange(doc:Document)和dosomethingonloyonpagechange(Document:doc,page:number)。这样做的目的不是在其他人的状态发生变化时执行处理程序。我已签署如下状态更改: this.store$.se
export interface DocumentState {
document: Document;
selectedPage: number;
}
我想调用dosomethingonloyondocumentchange(doc:Document)
和dosomethingonloyonpagechange(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选择器的原因
首先为所需的状态片创建选择器:
export const selectDocument = createFeatureSelector<AppState,
DocumentState>('document');
export const getDocument = createSelector(
selectDocument,
(state: DocumentState) => state.document
);
这样,只有对文档切片所做的更改才会由doSomethingOnDocumentChange
参考:
this.store$.select(getDocument).subscribe(this.DoSomethingOnlyOnDocumentChange);