简体   繁体   English

在 Angular 的嵌套子组件中传递数据的最佳方法是什么?

[英]What could be the best approach to pass data in nested child components, in Angular?

I have a scenario, where for example我有一个场景,例如

<parent-comp>
 <side-bar></side-bar>
 <item-details></item-details>
</parent-comp>

<side-bar>
 <item-list></item-list>
</side-bar>

I need to share items list into the components 'ItemListComponent' and 'ItemDetailsComponent', keeping the hierarchy in mind我需要将项目列表共享到组件“ItemListComponent”和“ItemDetailsComponent”中,并牢记层次结构

Approach 1方法一

I can fetch the items from ItemService in ParentComponent and send it via attribute/property <sidebar [pages]="pages"></sidebar> >>> <item-list [pages]="pages"></item-list> and <item-details[pages]="pages"></item-details>我可以从 ParentComponent 中的 ItemService 获取项目并通过属性/属性发送它<sidebar [pages]="pages"></sidebar> >>> <item-list [pages]="pages"></item-list><item-details[pages]="pages"></item-details>

I need to pass data through SidebarComponent to transfer it to ItemListComponent.我需要通过 SidebarComponent 传递数据以将其传输到 ItemListComponent。

Approach 2方法二

Fetching items from ItemService in the ParentComponent and instead of passing via attribute/property it emits the data (RxJs) let say with event name 'ItemDataFetched', and ItemList/ItemDetails component subscribed to that event.从 ParentComponent 中的 ItemService 获取项目,而不是通过属性/属性传递,它发出数据(RxJs),比如事件名称“ItemDataFetched”,以及订阅该事件的 ItemList/ItemDetails 组件。

What will be the best approach among the two?两者中最好的方法是什么? or is there any other better approach?或者还有其他更好的方法吗?

Check here: https://angular.io/guide/component-interaction the ways proposed by the angular team.在此处查看: https : //angular.io/guide/component-interaction angular 团队提出的方法。

I'd say input binding is the way to go in most cases.我会说输入绑定是大多数情况下的方法。

  1. It is the standard way of patent child communication.这是专利儿童通信的标准方式。 It will be what's expected by future developers to see, making debugging easier.这将是未来开发人员所期望看到的,使调试更容易。
  2. Event passing data is to be avoided in general.通常要避免事件传递数据。 If you are not using event binding with emitter for child to parent communication, you are out of the angular cycle and can cause unexpected problems.如果您没有使用带有发射器的事件绑定来进行子级到父级的通信,那么您将脱离角度循环并可能导致意外问题。
  3. Using a service is always an option.使用服务始终是一种选择。 Bear in mind though that services are singletons.请记住,尽管服务是单例的。 Thus have their own limitations.因而有其自身的局限性。

By the way, I would expect the child component to inform the parent of the selection and pass only the selected item's page to the item-details component.顺便说一句,我希望子组件通知父组件选择并仅将所选项目的页面传递给项目详细信息组件。 Not the entire collection.不是整个集合。

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

相关问题 Angular 4 - 填充子组件数据的最佳方式 - Angular 4 - Best way to populate child components data 在 Angular 中呈现数据的最佳方法是什么? - What is the best approach to render data in Angular? Angular - 检索数据的最佳方法是什么 - Angular - what is the best approach in retrieving data 在所有子组件和子子组件中的数据到达Angular中之前,显示加载栏的最佳方法是什么 - What is the best way to show loading bar until data in all child and sub child components arrive in Angular 将数据传递给 Angular 中的子组件数组 - Pass data to Array of Child Components in Angular 将解析器数据传递给子组件:Angular - Pass resolver data to child components : Angular 从嵌套的Component Angular 2中传递数据 - Pass data from nested Components Angular 2 什么是将数据传递到其他组件的最佳方法 - What is the best way pass data to other components angular 中父组件和子组件的最佳文件夹结构是什么 - what is the best folder structure for parent and child components in angular in Angular 13、如何通过依赖解析调用内部子组件到父组件? 刷新网格数据的最佳方法是什么? - in Angular 13, How can i call inner child component to parent component with dependency resolve ? what is the best approach to refresh grid data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM