我正在做應用程序中的國際化,父組件內部有很多組件。如何將父組件變量通知給其他組件?
在app.component.ts,我設置當前所選貨幣如下,
ngOnInit() {
this.selectedLanguage = "EN";
this.selectedCurrency = "EUR";
//also setting to local storage
localStorage.setItem('currency', this.selectedCurrency);
}
還有另外一個子組件在那裏我必須使用selectedcurrency在管,所以我得到在selectedcurrency在如下
export class EveComponent implements OnInit {
selectedCurrency :string;
ngOnInit() {
this.selectedCurrency = localStorage.getItem("currency");
}
}
,但這似乎並不工作,當變化發生的母公司。 和我的管如下,
<div class="price">{{123 | currConvert | currency:selectedCurrency:true:'3.2-2'}}</div>
現在,當我改變我的父頁面上selectedCurrency,我想,要在EveComponent應用也是如此。如何用angular2做到這一點?
LocalStorageService不是服務其組件 – Sajeetharan
我該如何修改?它保留一個錯誤,說$ currencyChanges無法找到 – Sajeetharan
你是什麼意思localStorage不是一項服務?那是什麼?你能分享它的代碼嗎? – Milad