首页 > 解决方案 > ngx 翻译 Angular 5

问题描述

当我在 settings.ts (页面)中更改语言并在同一页面中检查 currentLanguage 时,它​​会正确更改语言并显示它。

但是当我在 app.component.ts 中做同样的事情时,即使我改变了 10 次,CurrentLanguage 也不会改变。

这是代码。语言更改按钮在 settings.html 中

设置.ts

import { TranslateService } from '@ngx-translate/core';

constructor(private translate: TranslateService){
   //code...
}
changeLang(lang){
  this.translate.use("en");
  console.log("Switched to ", this.translate.currentLang);
  this.translate.use("ru");
  console.log("Switched to ", this.translate.currentLang);
} 

app.component.ts

import { TranslateService } from '@ngx-translate/core';


constructor(private translate: TranslateService){
   //code...
}

this.translate.use("ru");
  console.log("it is   ", this.translate.currentLang);
this.translate.use("en");
  console.log("it is   ", this.translate.currentLang);
this.translate.use("ru");
  console.log("it is   ", this.translate.currentLang);

settings.ts 的输出

Switched to  en 
Switched to  ru

app.component.ts 的输出

it is    to ru
it is    to ru
it is    to ru

标签: javascriptangularionic3ngx-translate

解决方案


推荐阅读