2016-09-06 53 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