【问题标题】:ngx-translate doesn't work in component.tsngx-translate 在 component.ts 中不起作用
【发布时间】:2019-10-15 01:27:56
【问题描述】:

我不明白为什么我无法在组件中获得翻译(在模板中可以正常工作)

我正在使用: 角 4.4 我尝试 ngx-translate@7.2 && @8 但它的行为就像翻译不存在

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

constructor(private translation: TranslateService) {
this.translation.get("DossierMedicale")
    .subscribe(value => {
        //returns DossierMedicale but in template it's correct
        console.log("aaaa", value);
    });

    //returns DossierMedicale but in template it's correct
    console.log("t2", this.translation.instant("DossierMedicale"));
}

【问题讨论】:

    标签: angular ngx-translate


    【解决方案1】:

    试试这个,如果它不起作用,请告诉我:

    import { TranslateService } from "@ngx-translate/core";
    
    constructor(
       private translation: TranslateService,
    ) {}
    
    ngOnint() {
       console.log(this.getTranslation('DossierMedicale'));
    }
    
    getTranslation(wordYouNeedToTranslate: string): string {
       return this.translate.instant(wordYouNeedToTranslate);
    }
    

    只要使用即时方法,如果这不起作用,请做一个stackblitz,我会解决的。

    P.S: 重启你的cli...

    【讨论】:

      【解决方案2】:

      试试这个代码:

      this.translate.stream('DossierMedicale').subscribe(value => console.log("aaa" + value))
      

      通过使用.stream,如果您已将加载程序设置为获取带有翻译的远程 .json,则无需进行额外的 http 调用。

      【讨论】:

        猜你喜欢
        • 2018-04-01
        • 1970-01-01
        • 2018-01-14
        • 2020-09-24
        • 1970-01-01
        • 1970-01-01
        • 2021-05-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多