현재 ngrx를 사용하여 축소 형 원칙을 사용하여 Angular로 응용 프로그램을 개발 중입니다.ngrx에서 상태가 변경 될 때 구성 요소 논리를 호출합니다.
이 상태에 따라 상태 변경에 반응하고 일부 구성 요소 로직을 호출하는 모범 사례를 찾고 있습니다.
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 ...
}
을
reducers.ts : 나는 내 말은 분명 무엇을 할 당신에게 (간체) 예를 줄 것이다 선택기 (getViewTypeOrFilterChanged) 내에서 상태의 세 가지 슬라이스를 결합하기 위해 reselct를 사용 중입니다. 이 선택기에 가입하면 결합 된 상태에 따라 몇 가지 작업을 수행하려고합니다.
사실, 여기에 게시/구독 방식으로 ngrx 스토어와 구독을 사용하는 느낌이 들지만 느낌이 정확하지 않습니다. 또한 ngOnInit의 구독 (nwOnInit)과 ngOnDestroy의 구독 취소 (nwOnDestroy)의 구독 (Subscriptions)은 문제가되지만, 예를 들어 다음을 사용하여 동일한 결과를 얻는 방법을 생각할 수 없습니다. 비동기 파이프. (결합 된) 상태 변화에 반응하는 좀 더 우아한 방법이 있습니까?
감사합니다.