我正在做一个项目,需要使用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');发布于 2020-05-13 18:20:56
通过使用only then而不使用console.log,您可以在评估的promise中等待
console.log('start');
this.translateService.stream('Labels').toPromise().then(labels => {
console.log(labels, 'check translation');
console.log('end');
});https://stackoverflow.com/questions/61771970
复制相似问题