状态的改变我目前正在开发使用Redux的原理与NGRX角的应用程序组件的逻辑。呼叫时NGRX
我在寻找一个最佳实践的反应状态的改变,并呼吁根据这个国家的一些组件的逻辑。我给你一个(简化)为例作清楚我的意思:
reducers.ts
import {createSelector} from 'reselect';
export const getViewTypeOrFilterChanged = createSelector(isLoading, getActiveViewType, getActiveFilter, (isLoading, activeViewType, activeFilter) => {
// ensure that data is loaded
if (!isLoading) {
return {
activeViewType: activeViewType,
activeFilter: activeFilter
};
}
});
例如,component.ts
@Component({ ... })
export class ExampleComponent implements OnInit {
// properties ...
constructor(private store: Store<fromRoot.AppState>) {
}
ngOnInit() {
this.subscriptions.push(
this.store.select(fromRoot.getViewTypeOrFilterChanged).subscribe((result) => {
if (result) {
this.property1 = result.activeType;
this.dependentHelperClass.method1(result.activeFilter);
this.method1();
this.method2(result.activeFilter);
this.method3();
}
})
);
}
ngOnDestroy() {
this.subscriptions.forEach((subscription: Subscription) => {
subscription.unsubscribe();
});
}
// methods ...
}
,你可以请参阅我还使用reselct在选择器(getViewTypeOrFilterChanged)中组合三个不同的状态片。在订阅这个选择器时,我想根据组合状态采取一些行动。
的事情是,我感觉像在多发布/订阅此模式的方式使用NGRX存储和订阅,感觉不是很正确。另外,订阅(我有多个的)在ngOnInit和取消订阅的ngOnDestroy打扰我,但我不能想办法实现使用例如相同的结果异步管道。 对于(组合)状态变化可能有更优雅的反应方式吗?
谢谢!