简体   繁体   English

Angular 2组成部分:儿童与儿童之间的沟通

[英]Angular 2 components: child to child communication

I have three components in an Angular 2 app: C0, C1 and C2. 我在Angular 2应用程序中有三个组件:C0,C1和C2。 The first one (C0) represents an html template, having multiple child components (ui elements). 第一个(C0)表示一个html模板,具有多个子组件(ui元素)。 Now, if someone clicks on a button in C1 (menu), how may I notify C2 (calendar) about that? 现在,如果有人点击C1 (菜单)中的按钮,我该如何通知C2 (日历)呢?

在此输入图像描述

I tried some examples from the component communication page from the angular site. 我尝试了角度站点的组件通信页面中的一些示例。 I ended up with an approach, where I took an EventEmitter at C1 , to notify the parent C0. 我最终得到了一种方法,我在C1处获取了一个EventEmitter ,以通知父C0。 And then used a setter to notify C2 . 然后使用setter通知C2

This works, but seems very messy, even for a hand full of events. 这有效,但看起来非常凌乱,即使对于充满活动的手。 This cant be the solution, if my app might have hundreds of events in the end. 如果我的应用程序最终可能有数百个事件,那么这个解决方案就无法解决。

Is there a better way, to handle such kind of communication? 有没有更好的方法来处理这种沟通?

I think you should add an EventEmitter to C1 as you mentioned. 我认为你应该像你提到的EventEmitterC1添加一个EventEmitter
You can bind to a method on C2 without involving C0 by adding a template variable to C2 and refer to it using it like: 您可以通过向C2添加模板变量来绑定到C2上的方法而不涉及C0 ,并使用它来引用它:

<c1 (customEvent)="c2.onClick($event)"></c1>
<c2 #c2></c2>

You can create one service which is shared between your child component in which you can define Observable so that you can subscribe to that Observable from one child and perform some action when you receive some value from another child. 您可以创建一个在您的子组件之间共享的服务,您可以在其中定义Observable以便您可以从一个子项订阅该Observable ,并在从另一个子项接收某个值时执行某些操作。 you can check https://stackoverflow.com/questions/39738974#39739184 which is used for parent to child communication but you can use same approach for child to child communication. 您可以查看用于父母与子女沟通的https://stackoverflow.com/questions/39738974#39739184 ,但您可以使用相同的方法进行儿童与儿童的沟通。

Is there a better way, to handle such kind of communication? 有没有更好的方法来处理这种沟通?

Depends on what you consider as better ;). 取决于你认为更好的东西;)。

There are some methods which runs, you just need to choose the flow of data which is more understandable and seems to work better for you. 有一些方法可以运行,你只需要选择更容易理解的数据流并且似乎更适合你。

For example you can use Service to communicate between those two Components. 例如, 您可以使用Service在这两个组件之间进行通信。 This is the way I do it, I implement all communication logic in Services, so my Components are only for presentation logic. 这就是我这样做的方式,我实现了Services中的所有通信逻辑,因此我的组件仅用于表示逻辑。

Is there a better way, to handle such kind of communication? 有没有更好的方法来处理这种沟通?

You can perfectly tackle this problem with events, but these kind of problems tend to get easier if you use redux (or in angular2, ngrx/store). 您可以通过事件完美地解决这个问题,但是如果您使用redux(或在angular2,ngrx / store中),这些问题往往会变得更容易。 There are many good articles and videos about the subject, and while it might look a bit strange at first, it becomes very natural after a while. 有很多关于这个主题的好文章和视频,虽然起初看起来有点奇怪,但一段时间后变得非常自然。

Google around and watch some videos, its a very interesting way of working, and you'll also start to understand the role of rxjs and how it can help in your application. Google围绕并观看一些视频,这是一种非常有趣的工作方式,您也将开始了解rxjs的作用以及它如何在您的应用程序中提供帮助。

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

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