現在、ngrxでreduxの原理を使用してAngularでアプリケーションを開発しています。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 ...
}
することができますように:私は、私が何を意味するかを明確にするためにあなたに(簡体字)の例をあげますセレクタ(getViewTypeOrFilterChanged)内で状態の3つのスライスを結合するためにreselctも使用していますを参照してください。このセレクタへのサブスクリプションでは、結合された状態に従っていくつかのアクションを実行したいと考えています。
ここでは、ngrxストアとサブスクリプションをパブリッシュ/サブスクライブパターンで使用するような気がしています。また、ngOnInitのサブスクリプション(複数ある)とngOnDestroyのサブスクリプションも気になりますが、私は同じ結果を得る方法を考えていません。非同期パイプ。 (組み合わせた)状態変化に反応するよりエレガントな方法がありますか?
ありがとうございます!