Angular 使用动态ngrx选择器或ngrx操作以获得更好的性能?
我将根据参数从存储中获取数据Angular 使用动态ngrx选择器或ngrx操作以获得更好的性能?,angular,redux,store,ngrx,Angular,Redux,Store,Ngrx,我将根据参数从存储中获取数据 export const selectSchedulingsTimes = createSelector( schedulings, (state: ISchedulesState, { time }: { time: string }) => { let nowFormat = moment(time, 'HH:mm'); return state.schedulings.data.filter(elm => { l
export const selectSchedulingsTimes = createSelector(
schedulings,
(state: ISchedulesState, { time }: { time: string }) => {
let nowFormat = moment(time, 'HH:mm');
return state.schedulings.data.filter(elm => {
let before = moment(elm.tp_org_r, 'HH:mm:ss');
let after = moment(elm.tp_des_r, 'HH:mm:ss');
let checkifBetween = nowFormat.isBetween(before, after);
if (checkifBetween) {
return elm;
}
});
}
);
我已经创建了选择器,但是使用了静态参数
export const selectSchedulingsTimes = createSelector(
schedulings,
(state: ISchedulesState, { time }: { time: string }) => {
let nowFormat = moment(time, 'HH:mm');
return state.schedulings.data.filter(elm => {
let before = moment(elm.tp_org_r, 'HH:mm:ss');
let after = moment(elm.tp_des_r, 'HH:mm:ss');
let checkifBetween = nowFormat.isBetween(before, after);
if (checkifBetween) {
return elm;
}
});
}
);
然后在我的组件中传递参数
export class LineSynopticComponent implements OnInit, AfterViewInit {
schedules$ = this.store.pipe(select(selectSchedulingsTimes, { time: '10:34' }));
ngOninit(){
}
因为schedules$
没有在它里面声明,如果我设置一个变量而不是10:34
它会工作吗
使用减速机是否更好?返回值或选择器的性能更高?使用参数调用选择器没有问题。只要在调用选择器时确保参数存在。我会这样做:
- 声明你的可观察性
- 在参数可用的地方调用选择器,我使用了onInit,因为这是您提供的唯一内容
schedules$: Observable<any> // whatever type it should be returning ngOninit(){ this.schedules$ = this.store.pipe(select(selectSchedulingsTimes, { time: '10:34' })); }
schedules$:Observable//它应该返回的任何类型 恩戈尼尼特(){ this.schedules$=this.store.pipe(select(selectSchedulingTimes,{time:'10:34'})); }