我有包含以下一個父容器:角2 - 從子傳遞(內路由器出口)的數據到父組件
<div id="pageContainer">
<router-outlet></router-outlet>
</div>
<div>
<div class="title">{{text}}</div>
</div>
我也有我打算使用通過從數據的服務子組件(路由器出口內)到父組件:
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class NotificationsService {
emitChange(change: any) {
return change;
}
}
的子組件調用服務方法:
this.notificationsService.emitChange('Data from child');
個
然後父組件受讓人{{文本}}從服務:
constructor(
private notificationsService: NotificationsService
) {
notificationsService.emitChange(
text => {
console.log(text);
});
}
但是,這仍然無法正常工作,任何想法?
父組件中是否有文本屬性? – DeborahK
爲什麼你想在路由器插座內傳遞數據?有沒有特定的原因?如果你的子組件用在你的父組件中,你可以使用一個EventEmitter來傳遞數據。 –
@KerimEmurla因爲我必須在整個應用程序中顯示一個通知框,並且從任何地方都必須傳遞一些數據,這似乎是有意義的。 –