Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular NgRx动态开关外壳行为参数化选择器_Angular_Redux_Rxjs_Ngrx - Fatal编程技术网

Angular NgRx动态开关外壳行为参数化选择器

Angular NgRx动态开关外壳行为参数化选择器,angular,redux,rxjs,ngrx,Angular,Redux,Rxjs,Ngrx,我有一个组件,可以从不同的NgRx功能状态获取多个数据。我对RxJS不是很熟练,我很难确定数据流的顺序 以下代码在组件初始化时运行: //与UI相关的显示上下文 this.displayContextId$=this.store.select(selectCurrentDisplayContextId); this.displayContextId$.subscribe(); //与此组件的父级相关的数据 this.currentItem$=this.store.select(selectCur

我有一个组件,可以从不同的NgRx功能状态获取多个数据。我对RxJS不是很熟练,我很难确定数据流的顺序

以下代码在组件初始化时运行:

//与UI相关的显示上下文
this.displayContextId$=this.store.select(selectCurrentDisplayContextId);
this.displayContextId$.subscribe();
//与此组件的父级相关的数据
this.currentItem$=this.store.select(selectCurrentItem);
//一些来自国家的数据
this.featureAData$=this.store.select(featureADataByItemId(this.subItem.id));
this.featureBData$=this.store.select(selectfeatureBDataByItemId(this.subItem.id));
此.featureBData$.subscribe();
//需要提取featureAData的某些数据
this.featureCData$=this.featureAData$.pipe(
mergeMap(featureA=>this.store.select(selectSomeDataById(featureA.someId)))
);
this.featureCData$.subscribe();
伪代码描述了我试图实现但目前无法实现的目标的本质:

//当displayContextId更改时,从存储区中选择一个特定项,该项由其他id(来自数据模型规范)的串联发出的id标识
此.displayContextId$.pipe(
).订阅(displayContextId=>{
开关(displayContextId){
case DisplayContexts.TypeA:
this.displayContextItem$=this.featureAData$.pipe(
合并映射(featureA=>this.store.select(selectDisplayContextItemById(displayContextId+'-'+featureA.someId)))
);
打破
case DisplayContexts.TypeB:
控制台日志(d);
this.displayContextItem$=this.featureCData$.pipe(
mergeMap(featureC=>this.store.select(SelectDisplayContextItemById(displayContextId+'-'+featureC.someId)))
);
打破
}
});
this.displayContextItem$.subscribe();
我很清楚,这不是RxJS的做法。我的目标是根据组件生命周期中不断变化的不同输入源,动态地从存储中重新选择数据

另外,我不知道在组件生命周期中应该把它放在哪里

这项技术的有效实施是什么


非常感谢,

尝试这样做:

import { switchMap } from 'rxjs/operators
....
this.displayContextItem$ = this.displayContextId$.pipe(
  // take the value of displayContextId and switch to a new observable depending on its value
  switchMap(displayContextId => {
    switch (displayContextId) {
        case DisplayContexts.TypeA:
            return this.featureAData$.pipe(
                mergeMap(featureA => this.store.select(selectDisplayContextItemById(displayContextId + '-' + featureA.someId)))
            );
        case DisplayContexts.TypeB:
            console.log(d);
            return this.featureCData$.pipe(
                mergeMap(featureC => this.store.select(selectDisplayContextItemItemById(displayContextId + '-' + featureC.someId)))
            );
    }
  }),
);
// after you initialized it, you can subscribe to it
this.displayContextItem$.subscribe();

您是否考虑过使用高阶选择器?高阶选择器仅适用于比
子项
高一级的
当前项
,而且当前项ID存储在可以调用
选择当前项
的状态。由于该组件用于许多不同的
子项
(我编辑了代码以使其更清晰),因此无法在状态中存储假设的
selectedSubItemId
,以便在高阶选择器中使用。