我正在做应用程序中的国际化,父组件内部有很多组件。如何将父组件变量通知给其他组件?
在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