组合多个/冗余ngrx Store select语句

时间:2018-06-26 11:41:46

标签: angular angular5 ngrx angular6 ngrx-store

我正在使用ngrx / angular4 +开发应用程序。 在这里开发的设计模式似乎非常多余,这与您订阅Store Reducer的方式有关。

在组件中,以下情况并不罕见:

HOFDetails$:Observable<any>;
HOFDetails$SUB:ISubscription;
HOFMobile$:Observable<any>;
HOFMobile$SUB:ISubscription;
HOFMobile:any;
HOFHome$:Observable<any>;
HOFHome$SUB:ISubscription;
HOFHome:any;
SPDetails$:Observable<any>;
SPDetails$SUB:ISubscription;
SPMobile$:Observable<any>;
SPMobile$SUB:ISubscription;
SPMobile:any;
SPHome$:Observable<any>;
SPHome$SUB:ISubscription;
SPHome:any;
EmergencyDetails$:Observable<any>;
EmergencyDetails$SUB:ISubscription;
EmergencyDetails:any;
EmergencyMobile$:Observable<any>;
EmergencyMobile$SUB:ISubscription;
EmergencyMobile:any;
EmergencyHome$:Observable<any>;
EmergencyHome$SUB:ISubscription;
EmergencyHome:any;
EmergencyList$:Observable<any>;
EmergencyList$SUB:ISubscription;
EmergencyList:any;
countries$:Observable<any>;
countries$SUB:ISubscription;
countries:any;
valueIds$:Observable<any>;
valueIds$SUB:ISubscription;
valueIds:any;
lang$:Observable<any>;
lang$SUB:ISubscription; 
//the subscriptions
    this.lang$ =  this.store.select(fromroot.getLanguage);
    this.lang$SUB = this.lang$.subscribe((v:string)=>this.lang = v);
    this.HOFDetails$ = this.store.select(from_costumer.getHOF);
    this.HOFDetails$SUB = this.HOFDetails$.subscribe((v)=> {});
    this.HOFMobile$ = this.store.select(from_costumer.getHOFMobile);
    this.HOFMobile$SUB = this.HOFMobile$.subscribe((v)=> {});
    this.HOFHome$ = this.store.select(from_costumer.getHOFHome);
    this.HOFHome$SUB = this.HOFHome$.subscribe((v)=> {});
    this.SPDetails$ = this.store.select(from_costumer.getSP);
    this.SPDetails$SUB = this.SPDetails$.subscribe((v)=> {});
    this.SPMobile$ = this.store.select(from_costumer.getSPMobile);
    this.SPMobile$SUB = this.SPMobile$.subscribe((v)=> {});
    this.SPHome$ = this.store.select(from_costumer.getSPHome);
    this.SPHome$SUB = this.SPHome$.subscribe((v)=> {});
    this.EmergencyDetails$ = this.store.select(from_costumer.getEmergency);
    this.EmergencyDetails$SUB = this.EmergencyDetails$.subscribe((v)=>this.EmergencyDetails = v);
    this.EmergencyList$ = this.store.select(from_costumer.getEmergencyList);
    this.EmergencyList$SUB = this.EmergencyList$.subscribe((v:any)=> {});
    this.EmergencyMobile$ = this.store.select(from_costumer.getEmergencyMobile);
    this.EmergencyMobile$SUB = this.EmergencyMobile$.subscribe((v:any)=> {});
    this.EmergencyHome$ = this.store.select(from_costumer.getEmergencyHome);
    this.EmergencyHome$SUB = this.EmergencyHome$.subscribe((v:any)=> {});
    this.countries$ = this.store.select(from_geo.getCountries);
    this.countries$SUB = this.countries$.subscribe((v:any)=> this.countries = v);
    this.valueIds$ = this.store.select(from_geo.getCountryIds);
    this.valueIds$SUB = this.valueIds$.subscribe((v:any)=>this.valueIds = v);

此代码仅针对上的 13个属性,不包括订阅和退订中的分配逻辑。

在我看来,这是令人难以置信的重复和多余。

是否有办法将所有这些打包成某种可重复使用的软件包? 也许基于组件还是基于路由?

2 个答案:

答案 0 :(得分:2)

删除订阅,删除未打包的变量(例如countries:any;)。您剩下的只有Observables。将它们放在容器(智能)组件中,然后使用async管道将这些可观察对象绑定到您(哑)表示性组件。

例如:

lang$ : Observable<whatevertype>;

constructor(){
   this.lang$ =  this.store.select(fromroot.getLanguage);
}

在模板中:

<your-presentational-component [lang]="lang$ | async"><your-presentational-component>

然后async管道接替订阅,取消订阅并将实际值分配给实际使用它们的子组件。

答案 1 :(得分:1)

将其缩短到您的容器组件中

// inject ngrx store in constructor
constructor(private store: Store) {}

// lang$ as class property, no extra code needed.
lang$: Observable<any> = this.store.select(fromroot.getLanguage);

您可能不需要设置本地属性this.lang,只需将可观察对象传递给您的任何表示组件并使用异步管道即可。