angular - 点击语言作品 1 次 3
问题描述
例如,当我选择一种语言时NL
,我必须单击 2 次或 3 次才能以荷兰语正确显示文本。
我不明白为什么第一次点击不起作用?
HTML
<div class="languageSelect">
<a href="#" *ngFor="let l of supportedLangs;" (click)="switchLanguage(l); false; ">
{{ l | uppercase}}
</a>
</div>
TS
我认为问题出在我的打字稿上?
@Component({
selector: 'app-auth-layout',
templateUrl: './auth-layout.component.html',
styleUrls: ['./auth-layout.component.scss']
})
export class AuthLayoutComponent implements OnInit {
supportedLangs;
constructor(
public _router: Router,
private translate: TranslateService,
private LS: LocalStoreService,
public _location: Location
) { }
ngOnInit() {
this.supportedLangs = ['fr', 'nl', 'en', 'de', 'es', 'it', 'pt'];
}
switchLanguage(lang) {
if (lang == 'uk') {
lang = 'en';
}
this.translate.onLangChange.subscribe((event: LangChangeEvent) => {
this.LS.setItem('LX_Current_Language', event.lang);
});
this.translate.use(lang);
this.refresh();
}
refresh(): void {
this._router.navigateByUrl("/refresh", { skipLocationChange: true }).then(() => {
this._router.navigate([decodeURI(this._location.path())]);
});
}
}
解决方案
我认为您必须像这样添加更改检测器:
你的构造函数
constructor(
public _router: Router,
private translate: TranslateService,
private changeDetectoref : ChangeDetectorRef,
private LS: LocalStoreService,
public _location: Location) { }
切换语言功能
switchLanguage(lang) {
if (lang == 'uk') {
lang = 'en';
}
this.translate.onLangChange.subscribe((event: LangChangeEvent) => {
this.LS.setItem('LX_Current_Language', event.lang);
});
this.translate.use(lang);
this.changeDetectorRef.detectChanges();
this.refresh();}
推荐阅读
- php - 使用 php Laravel 进行高级搜索的问题
- c# - 如何从多个组合框填充数组?
- r - 如何在不丢失行的情况下创建 dtm
- php - 为什么 PDO query() 结果的键包含表名?
- c++ - 为什么 std::equal 模板由两个类参数化?
- python - 我如何比较 2 个字符串列表并返回布尔索引
- javascript - Chrome 最终关闭 - React JS
- c# - 有没有办法在建议 MVC 控制器的方法中有一个字符串参数?
- c# - 如何使用角色 ARN 连接到 S3 存储桶是 C#
- sharepoint-online - 如何使用 pnp.sp 人员选取器设置 SharePoint 列表字段