简体   繁体   English

ngrx store reducer配置(角度5)

[英]ngrx store reducer configuration (angular 5)

I am using ngrx/store in my Angular 5 project. 我在Angular 5项目中使用ngrx / store。 The Application state I store has multiple properties (slices). 我存储的应用程序状态具有多个属性(切片)。 I want to individually be able to listen to changes to any of those properties. 我希望能够单独听取对任何这些属性的更改。 So in this case, should I be using multiple reducers - one for each state slice? 因此,在这种情况下,我应该使用多个reducer-每个状态切片一个吗? Can it be achieved with one reducer? 一个减速器可以实现吗? I am guessing it cannot be done, because with one reducer we will return a new copy of the entire state, rather than a slice. 我猜它无法完成,因为使用一个化简器,我们将返回整个状态的新副本,而不是切片。 For eg 例如

class AppState{
    private customerList: Customer [];
    private selectedCustomer: Customer;
    private countriesOperational: Country [];
}

I want to be able to listen to changes only on selectedCustomer, so I can do this: 我希望只能在selectedCustomer上侦听更改,因此我可以这样做:

store.select(state => state.selectedCustomer).subscribe((data) => {
})

First of all - there is no need to have several reducers. 首先-不需要多个减速器。 The new reducer should be implemented once you feel that your current reducer is too big / has multiple resposibilities / should be splitted due to some constrains. 一旦您感觉到当前的减速器太大/具有多个可重构性/由于某些限制而应拆分,则应实施新的减速器。

Going back to your problem - lets say that your customer has 'id' property. 回到您的问题上-假设您的客户拥有“ id”属性。 And in the scenario that I want to present, the app will be showing list of current ids - from customerList. 在我要演示的场景中,该应用程序将显示当前id的列表-来自customerList。 The customerList is going to be dynamically updated using ngrx actions (and the template will be listening to the changes). 客户清单将使用ngrx动作进行动态更新(模板将监听更改)。

In component: 在组件中:

public customerIds$: Observable<string[]>;

public ngOnInit(): void {
   this customerIds$ = this.store.select(
      state => state.customersList.map(customer => customer.id);
   );
}

In your template: 在您的模板中:

<div *ngFor="let id of customerIds$ | async">
   {{id}}
</div>

Right now (using async pipe) you connected your html template with ts component. 现在(使用异步管道),您将html模板与ts组件连接。 So, lets say that you have a button that is adding a new customer to customersList: 因此,假设您有一个将新客户添加到customersList的按钮:

<button (click)="addNewCustomer()">Add new customer</button>

And the addNewCustomer() method is dispatching an action, that is handled by your store. addNewCustomer()方法正在调度一个动作,该动作由您的商店处理。 The result of an action is hiddin in reducer, sth like that: 动作的结果是在reducer中隐藏,诸如此类:

... (reducer logic)
   case ADD_NEW_CUSTOMER:
      return {
        ...state,
        customersList: [...state.customersLits, action.payload]

Where: 哪里:

Once the button is clicked, new customer id is displayed in the template 单击按钮后,新的客户ID将显示在模板中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置@ ngrx / store的reducer到设置{[key:string]:AnyComponent}的角度6中 - How to set reducer for @ngrx/store in angular 6 that set {[key:string]:AnyComponent} 被特效捕获的Angular 6 Ngrx存储动作仍归入reducer - Angular 6 ngrx store action caught by effects still fall into reducer 如何为Angular ngrx商店制作一个“reducer enhancer”来进行撤销/重做 - How to make a “reducer enhancer” for Angular ngrx store for undo / redo Angular 8 Ngrx存储如何在没有订阅回调的情况下获取reducer的值? - Angular 8 ngrx store how to get reducer value without subscribe callback? 如何在ngrx / Store reducer中使用其他Angular2服务? - How to use other Angular2 service inside an ngrx/Store reducer? 如何修改NGR存储不在reducer中? - How to modify NGRX store not in reducer? NGRX angular 5减速机配AOT编译器 - NGRX angular 5 reducer with AOT compiler Angular 2,ngrx,测试我的Reducer - Angular 2, ngrx, testing my Reducer Angular 8:使用 ngRx/store 从 reducer 中获取的结果没有更新异步 (html) 中的 UI - Angular 8 : result fetched from reducer using ngRx/store is not updating the UI in async (html) Angular 6 Ngrx存储来自其他要素缩减程序正在侦听的要素的操作 - Angular 6 ngrx store action from a feature being listened by other feature reducer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM