【问题标题】:Wait until translation completed - Ngx Translate等到翻译完成 - Ngx 翻译
【发布时间】:2020-05-13 18:48:50
【问题描述】:

我正在开发一个项目,该项目需要使用 ngx 翻译库翻译 typescript 文件中的文本,但我想等到翻译完成后再加载组件。我尝试了以下代码,但没有成功。

console.log('start');
this.lables = await this.translateService.stream('Labels').toPromise().then(res => res);
console.log(lables , 'check translation');  // it show nothing 
console.log('end');

第二种方法:

console.log('start');
this.lables = await this.translateService.stream('Labels').pipe(take(1)).toPromise().then(res => res);
console.log(lables , 'check translation');  // it show "Labels" as a string.
console.log('end');

【问题讨论】:

    标签: javascript angular rxjs ngx-translate


    【解决方案1】:

    您可以在没有等待的情况下仅使用 then 来在已评估的 Promise 中进行 console.log

    console.log('start');
    this.translateService.stream('Labels').toPromise().then(labels => {
      console.log(labels, 'check translation');
      console.log('end');
    });
    

    【讨论】:

    • 我试过了,它把标签显示为字符串而不是内容
    • 您需要调查您的this.translateService.stream('Labels') 它返回了什么?
    • 它应该返回一个包含翻译单词列表的 json 对象
    • 它会这样做吗? consoe.log 它
    • 好吧,那么你需要看看如何使用 this.translateService.stream('Labels') 正确获取该对象
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    相关资源
    最近更新 更多