首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当lang在translateService.onLangChange.subscribe中发生变化时,OnInit不会触发

当lang在translateService.onLangChange.subscribe中发生变化时,OnInit不会触发
EN

Stack Overflow用户
提问于 2021-01-14 16:11:50
回答 1查看 2K关注 0票数 0

因此,我在一个组件中使用了translateService.onLangChange.subscribe,它可以很好地工作,但是如果它订阅langChange并为我设置一个localeDate,那么它就创建了一个单独的服务。但是,当我改变朗,它没有登记,什么也不做。

代码语言:javascript
运行
复制
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()中获得该值,而不需要

代码语言:javascript
运行
复制
this.translateService.onLangChange.subscribe(() => thisLocaleDateAdapterService.getLocaleDateFormat()); 

在我的那部分。我试着订阅它,但得到的错误是它不是一个函数。

这就是我试过的:

代码语言:javascript
运行
复制
this.localeDateAdapterService.getLocaleDateFormat().subscribe(currLang => this.currLang = currLang);

基本上,我希望它能够监听getLocaleDateFormat,并在其变化时更改其值。

更新:我刚刚意识到问题出在OnInit上,而不是破坏lang的变化。LangChange在函数中是在服务内部触发的,而在OnInit中则不是触发器。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-23 16:39:22

忘记将调用onLang的服务放在主模块提供程序中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65722610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档