角度2翻译json观察不到的工作 [英] angular 2 translation json observable does not work
问题描述
某人发布了plnkr,以演示如何通过服务下载json文件并从管道中使用它进行翻译
someone posted a plnkr to show how to have a json file downloaded via a service and use it from a pipe for translations
但是它仅适用于一种翻译,因为我对angular并不陌生,我发现很难理解/调试,我问有人是否可以指出我这段代码有什么问题
but it only works for one translation, as I am new to angular, and I find it really difficult to understand/debug, I am asking if someone can point to me what is wrong with this code
https://plnkr.co/edit/VMqCvX (我可以将其分叉,但确实可以不保存:-()
https://plnkr.co/edit/VMqCvX (I can fork it but it does not save :-( )
当我添加另一个要翻译的单词和另一个翻译时,只有最后一个被翻译
as I add another word to translate and another traduction, only the last one is translated
app/i18n/localizable.it.strings
app/i18n/localizable.it.strings
{
"home.nav.calendar": "Calendar"
"home.nav.test": "Test"
}
app/app.component.ts
app/app.component.ts
<span>{{'home.nav.calendar' | translate}}</span>
<span>{{'home.nav.test' | translate}}</span>
plz帮助
谢谢
推荐答案
问题是Observable仅对预订的最后一个值发出true.这是因为观察者没有在假设之间共享.您可以修改观察者以在订阅之间共享自己,以便订阅者都知道何时加载数据,如下所示:
The issue is that the Observable is only emitting true for the last value that subscribes to it. This is because the observer is not shared between supscriptions. You can modify the observer to share itself among subscriptions so that they all know when the data is loaded like so:
this.translationLoaded = Observable.create((observer) => {
this.translationLoadedObserver = observer;
}).share();
这篇关于角度2翻译json观察不到的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!