因此,我在一个组件中使用了translateService.onLangChange.subscribe,它可以很好地工作,但是如果它订阅langChange并为我设置一个localeDate,那么它就创建了一个单独的服务。但是,当我改变朗,它没有登记,什么也不做。
ngOnInit(): void {
this.getLocaleDateFormat();
this.translateService.onLangChange.subscribe(() => this.getLocaleDateFormat());
}
getLocaleDateFormat() {
// To add another lang it has to be added to Languages bellow to match the locale_id of the lang and in main module it has to be imported and registered
this.dateAdapter.setLocale(Languages[this.translateService.currentLang]);
this.currLang = Languages[this.translateService.currentLang];
console.log('locale-date-adapters currLang: ' + this.currLang);
return this.currLang;
}
另外,每当lang更改时,我希望我的组件从getLocaleDateFormat()中获得该值,而不需要
this.translateService.onLangChange.subscribe(() => thisLocaleDateAdapterService.getLocaleDateFormat());
在我的那部分。我试着订阅它,但得到的错误是它不是一个函数。
这就是我试过的:
this.localeDateAdapterService.getLocaleDateFormat().subscribe(currLang => this.currLang = currLang);
基本上,我希望它能够监听getLocaleDateFormat,并在其变化时更改其值。
更新:我刚刚意识到问题出在OnInit上,而不是破坏lang的变化。LangChange在函数中是在服务内部触发的,而在OnInit中则不是触发器。
发布于 2021-01-23 16:39:22
忘记将调用onLang的服务放在主模块提供程序中。
https://stackoverflow.com/questions/65722610
复制相似问题