2016-09-06 54 views
0

我想创建不是父母或孩子的组件之间的交互:它们处于不同的NgModules中。 我想象了一个服务,我注入了我的组件,但似乎不可能。而且我不能在另一个组件中注入我的组件(因为它们不是父/子)。但他们有一个共同的父母(AppComponent)。 我怎么能做我的组件沟通?Angular 2中的组件交互RC5

+0

https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service –

+0

我在寻找哪里组件不是家长或情况子女 – Pythorogus

+0

这就是链接文档所涵盖的内容。 –

回答

0

您一定可以使用服务。类似的东西:

import { EventEmitter } from '@angular/core'; 

export class SharedService { 
    pushedData = new EventEmitter<string>(); 
    private data: string[] = []; 

    addData(input: string) { 
     this.data.push(input); 
    } 

    getData() { 
     return this.data; 
    } 

    pushData(value: string) { 
     this.pushedData.emit(value); 
    } 
} 
+0

谢谢!你能举一个例子,2个组件与这个服务交互吗? – Pythorogus

+0

在这里找到我的解释:http://stackoverflow.com/questions/37579691/angular-2-component-to-component-communication?rq=1,thx for all – Pythorogus