Javascript 更新ngOnChanges内的ngrx选择器
我有一个父组件Javascript 更新ngOnChanges内的ngrx选择器,javascript,angular,rxjs,ngrx,ngrx-store,Javascript,Angular,Rxjs,Ngrx,Ngrx Store,我有一个父组件(B),它从它的父输入(a) (C)have是(B)子组件 在(B)中,我有一个从存储中获取数据的选择器 export class BComponent implements OnChanges { @Input() branchId; ngOnChanges() { this.selectedDataByBranch$ = this.store.pipe( select(selectBranchDirections, { branchId: th
(B)
,它从它的父输入(a)
(C)
have是(B)
子组件
在(B)
中,我有一个从存储中获取数据的选择器
export class BComponent implements OnChanges {
@Input() branchId;
ngOnChanges() {
this.selectedDataByBranch$ = this.store.pipe(
select(selectBranchDirections, { branchId: this.branchId, dir: this.selectedDirection })
);
this.selectedDataByBranch$.subscribe(selectedDataByBranch => {
this.trainsDatasets = this.getDatasets(selectedDataByBranch);
this.lineChart.data.datasets = this.trainsDatasets ? this.trainsDatasets : [];
this.lineChart.update();
});
directionChanged(event) {
this.selectedDirection = event;
this.selectedDataByBranch$ = this.store.pipe(
select(selectBranchDirections, { branchId: this.branchId, dir: this.selectedDirection })
);
}
}
directionChanged
是我从(C)
此问题的原因是selectedDataByBranch
subscription未在selectedDataByBranch$
我也试过这样做
directionChanged(event) {
this.selectedDirection = event;
select(selectBranchDirections, { branchId: this.branchId, dir: this.selectedDirection });
}
我的建议是。将参数转换为
主题
,然后与存储选择合并,在方向更改(事件)
方法中为主题提供值
因此,您的最终代码如下所示:
export class BComponent implements OnChanges {
@Input() branchId;
criterias$= new Subject<{branchId:number,dir:number}>;
ngOnChanges() {
this.selectedDataByBranch$ = this.criterias$.pipe(mergeMap(criteria=> this.store.pipe(
select(selectBranchDirections, { branchId: criteria.branchId, dir: this.searchDirection})
)));
this.selectedDataByBranch$.subscribe(selectedDataByBranch => {
this.trainsDatasets = this.getDatasets(selectedDataByBranch);
this.lineChart.data.datasets = this.trainsDatasets ? this.trainsDatasets : [];
this.lineChart.update();
});
this.criterias$.next({branchId:this.branchId,dir:this.sortDirection}); // init first call
}
directionChanged(event) {
this.selectedDirection = event;
this.criterias$.next({ branchId: criteria.branchId, dir: this.searchDirection}});
);
}
}
导出类b组件实现一次更改{
@输入();
准则$=新科目;
ngOnChanges(){
this.selectedDataByBranch$=this.criterias$.pipe(mergeMap(criteria=>this.store.pipe(
选择(selectBranchDirections,{BranchHid:criteria.BranchHid,dir:this.searchDirection})
)));
此.selectedDataByBranch$.subscribe(selectedDataByBranch=>{
this.trainsDatasets=this.getDatasets(selectedDataByBranch);
this.lineChart.data.datasets=this.trainsDatasets?this.trainsDatasets:[];
这个.lineChart.update();
});
this.criterias$.next({branchId:this.branchId,dir:this.sortDirection});//初始化第一次调用
}
方向更改(事件){
this.selectedDirection=事件;
this.criterias$.next({branchId:criteria.branchId,dir:this.searchDirection});
);
}
}
这试图实现我所说的