简体   繁体   English

Angular + ngRx 如何为新的 state 刷新组件

[英]Angular + ngRx how to Refreshing a component for a new state

I have a doubt about how to update a component, I am making a web app with Angular (lastest version) and I am using ngRx to manage the state.我对如何更新组件有疑问,我正在用 Angular(最新版本)制作一个 web 应用程序,我正在使用 ngRx 来管理 state。

my web has a Gridview with items where each item is a component (see the image)我的 web 有一个 Gridview,每个项目都是一个组件(见图)

Now, I want to update the data inside of ItemComponent, for example the first one item ( without add a subcriber in every Item component) but I don't know how to do it.现在,我想更新 ItemComponent 内部的数据,例如第一个项目(不在每个 Item 组件中添加订阅者)但我不知道该怎么做。

What is the best way to do this?做这个的最好方式是什么? I want to update any item totally independient between them because if I add a subscribe inside the ItemComponent then I will have to add an conditional to check the component id and exclude it if it's different我想更新它们之间完全独立的任何项目,因为如果我在 ItemComponent 中添加一个订阅,那么我将不得不添加一个条件来检查组件 ID 并在不同时将其排除

I hope I have explained correctly我希望我解释正确

在此处输入图像描述

  1. add a list of items for the grid to your ngrx store将网格的项目列表添加到您的 ngrx 商店
  2. grid view component selects that list of items for display网格视图组件选择要显示的项目列表
  3. grid view component passes each item to an item component网格视图组件将每个项目传递给项目组件
  4. when an item is updated from within an item component, you dispatch an action with the updated item as its payload当从项目组件中更新项目时,您将以更新的项目作为其有效负载发送一个操作
  5. the reducer handles the action, and updates the relevant item in the items list reducer 处理动作,并更新项目列表中的相关项目
  6. the store emits the complete list with the updated item商店发出包含更新项目的完整列表
  7. the grid view component updates to reflect the changes网格视图组件更新以反映更改

Grid view component template:网格视图组件模板:

<grid-view-item *ngFor="item of items$ | async" [item]="item"></grid-view-item>

Grid view component class:网格视图组件 class:

items$ = this.store.select(selectItems);

Grid view item component template:网格视图项组件模板:

<p>Name: {{ item.name }} </p>
// etc

Grid view item component class:网格视图项目组件 class:

@Input item: GridViewItem;

// call this depending on your implementation
onEdited(updatedItem: GridViewItem){
  this.store.dispatch(gridViewItemUpdatedAction(updatedItem));
}

Reducer减速器
Replace the updated item, leave the rest as they are:替换更新的项目,保留 rest 不变:

on(gridViewItemUpdatedAction, (state, { updatedItem }) => ({
  ..state,
  items: items.map(item => item.id === updatedItem .id ? updatedItem : item)
}),

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM