javascript - 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
解决方案
推荐阅读
- exchangewebservices - 无法将 SyncFolderHierarchy 操作到 archivemsgfolderroot
- python - 在python中随机分割一个给定块大小的列表
- exception - 如何在 Playwright-python 中处理 wait_for_selector 函数的 TimeoutError?
- javascript - Elementor 没有在 html 小部件上检测到我的图像。我的代码有错误吗?
- ios - 如何修复 UIPageViewController 手动分页?
- php - 如何在 Google App Engine 上运行 php 脚本?
- node.js - 如何启用在 Azure 容器实例中运行的 OPC UA 服务器?
- python - pytorch 张量板“add_embedding 错误”
- php - 更改 WordPress 中忘记密码页面的描述
- java - 如何在 PostConstruct 时在 Spring 中自动装配此依赖项