我正在使用 angular 7 的 redux。我可以将值添加到状态。但是我怎样才能订阅状态的变化值。
这是我尝试过的代码
选择器
export const selectProfessionalLearningTeacherFeature =
createFeatureSelector<any>('professionalLearningTeacher');
export const selectProfessionalLearningTeachers =
createSelector(selectProfessionalLearningTeacherFeature, (state) => state.professionalLearningTeacher);
运动组件
@Component({
selector: 'app-sports-practice',
templateUrl: './sports-practice.component.html',
styleUrls: ['./sports-practice.component.scss']
})
export class SportsPracticeComponent implements OnInit {
constructor(private fb: FormBuilder, private professionalLearningService: ProfessionalLearningService,private _store: Store<any>) {
this._store.select(selectProfessionalLearningTeachers)
.pipe(takeUntil(this._ngOnDestroy)).subscribe(item => {
console.log(item);
});
}
ngOnInit() { }
}
这是我的减速器组件
const meta: any = '';
const ProfessionalLearningTeachers: any = '';
const INITIAL_STATE: any = {
meta,
ProfessionalLearningTeachers
}
export function ProfessionalLearningTeacherReducer() {
return function entityReducer(
state: any = INITIAL_STATE,
a: Action,
): any {
const action = a as EntityAPIAction;
if (!action.meta) {
return state;
}
let itemIndex = -1;
switch (action.type) {
case ProfessionalLearningTeacherApiActions.PROFESSIONAL_LEARNING_TEACHER_SAVE_SUCCEEDED:
return storeProfessionalLearning(state, action);
}
return state;
};
}
function storeProfessionalLearning(state, action): any {
return Object.assign({}, state, {
meta: action.meta,
ProfessionalLearningTeachers: action.payload,
});
}
状态图
控制台输出
参考
在 Angular (v5) 中,我如何监听我的应用程序 Redux 状态对象的变化?
https://github.com/angular-redux/store/blob/master/articles/select-pattern.md