【发布时间】:2018-12-05 03:02:24
【问题描述】:
我正在使用 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);
所有这些代码仅适用于 this 上的 13 个属性,不包括订阅和取消订阅中的分配逻辑。
这在我看来是难以置信的重复和冗余。
有没有办法将所有这些打包成某种可重复使用的包? 可能是基于组件,还是基于路由?
【问题讨论】:
标签: angular angular5 ngrx angular6 ngrx-store