我在我的网站内使用 ngx-translate,唯一的问题是我知道如何在 html 模板内使用它,它工作得很好,但我如何调用 keyJSON
内部组件?
这就是我所需要的
app.html
<div>{{"home" | translate }}</div>
这工作正常,但在组件内部我有这样的东西
this.Items = [
{ title: 'Home', component: BookAServicePage, icon: 'settings' }
];
我怎样才能替换这个HOME以从a读取json
file?
我能够找到解决方案。需要注意的一件事是我必须利用 DoCheck 生命周期事件,以便组件级标签无需刷新即可翻译。
我是这样做的。
import { Component, DoCheck } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
@Component({
selector: 'app-some',
templateUrl: './some.component.html',
styleUrls: ['./some.component.css']
})
export class SomeComponent implements DoCheck {
headers = [];
constructor(public translate: TranslateService){
}
ngDoCheck() {
this.translate.get(['HEADER.PRIORITY', 'HEADER.STATE', 'HEADER.DATE_CREATED'])
.subscribe(translations => {
this.setHeaders(translations);
});
}
setHeaders(translations) {
this.headers = [
{
title: translations['HEADER.PRIORITY'],
active: true
},{
title: translations['HEADER.STATE'],
active: false
},{
title: translations['HEADER.DATE_CREATED'],
active: false
}];
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)