我正在尝试找到一种在组件和完全解耦的子组件之间创建连接的方法。
例如,如果我有两个组件,父组件
和子组件
,它们只存在于父组件
内部在标记中,我需要找到一种让 parent-component
和 child-component
进行双向通信的方法。
例如,如果我有以下标记:
<parent-component>
<child-component></child-component>
<child-component></child-component>
<child-component></child-component>
<child-component></child-component>
<child-component></child-component>
</parent-component>
我需要一种方法让父组件引用每个子组件的状态,以及每个子组件引用父组件的状态。
在 Angular 2 中有办法做到这一点吗?我到处都查过了,但确实找不到解决方案。
最佳答案
如果您希望 parent 和 child 能够相互交流,Angular2 建议使用一项服务。这在食谱中有介绍:https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service
关于javascript - 在两个解耦的组件之间创建连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41022115/