管道改造中的承诺

Ber*_*man 3 ionic-framework angular-pipe angular

我不想使用“ |异步管道”,因为我不想获取每个组件的数据。我的转换功能如下:

 transform(key: string, ...args) {
  this.storage.get("dictionary").then((dict) => {         
   var translated = dict[key] || "noResource";
   console.log("key = "+key + ", value = "+translated);
   return translated; 
  });  
}
Run Code Online (Sandbox Code Playgroud)

我可以获取键和值,但无法显示值。我尝试了ngZone但没有工作。

Pie*_*let 5

我不明白为什么您不希望使用唯一符合您情况的“ buildin”管道。

说您的管道是:

 @Pipe({name: 'mypipe'})
    export class MyPipe implements PipeTransform
    {
        transform(key: string, ...args) {
            // watch out you missed the return statement !!
            -->return<-- this.storage.get("dictionary").then((dict) => {         
            var translated = dict[key] || "noResource";
            console.log("key = "+key + ", value = "+translated);
            return translated; 
        });  
    }
Run Code Online (Sandbox Code Playgroud)

您可以在模板中使用

{{ 'mykey' | mypipe | async }}
Run Code Online (Sandbox Code Playgroud)

如果您不想使用异步管道,那么您将不得不模仿逻辑,这种逻辑已经很简单并且没有错误。没有为您带来收益。