Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript typescript中异步管道的等价物_Javascript_Angular_Typescript_Ionic2_Rxjs - Fatal编程技术网

Javascript typescript中异步管道的等价物

Javascript typescript中异步管道的等价物,javascript,angular,typescript,ionic2,rxjs,Javascript,Angular,Typescript,Ionic2,Rxjs,我在Ionic2中使用,它为我提供了在代码中翻译字符串的方法。目前,我必须使用如下服务: translate.get('ERROR').subscribe((res: string) => { //The string with code 'ERROR' is translated in res this.errorString = res; }); .... //Later on, when error happens: alert(this.errorString); 我

我在Ionic2中使用,它为我提供了在代码中翻译字符串的方法。目前,我必须使用如下服务:

translate.get('ERROR').subscribe((res: string) => {
   //The string with code 'ERROR' is translated in res
   this.errorString = res;
});
....
//Later on, when error happens:
alert(this.errorString);
我在许多文件中有许多字符串、警报和通知。订阅
get
方法对每个方法的可观察性是非常乏味的。在html中,使用异步管道或在本例中使用translate管道可以轻松避免这种购买,这不需要显式订阅observable:

<div>{{ 'ERROR' | translate}}</div>

考虑到代码在
async
函数中,它可以

this.errorString = await translate.get('ERROR').toPromise();

否则应使用
subscribe(…)

“不需要显式订阅可观察对象”-是的。这就是
| async
所做的!我知道这就是
| async
所做的。作为开发人员,使用该管道,我不必显式地编写订阅代码。我在代码中寻找这样的速记。你可以编写你自己的
asyncAlert
函数,比如,没有什么可以阻止你这么做。就像其他重构一样,只需提取公共代码即可。只是想知道,
等待
做什么?这个承诺会得到解决吗?@Ivaro18是的,会的。请参阅有关的详细信息。
this.errorString = await translate.get('ERROR').toPromise();