我正在一个需要使用ngx翻译库翻译打字稿文件中的文本的项目,但是我想等到翻译完成后再加载该组件。我尝试了以下代码,但没有用。
console.log('start');
const lables = await this.translateService.stream('Labels').toPromise().then(res => res);
console.log(lables , 'check translation'); // it show nothing
console.log('end');
第二方法:
console.log('start');
const 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');
您可以仅在不使用await的情况下使用console.log来评估承诺中的内容>
console.log('start');
this.translateService.stream('Labels').toPromise().then(labels => {
console.log(labels, 'check translation');
console.log('end');
});