无法找到管道:Angular 5自定义管道

时间:2018-01-25 19:44:21

标签: javascript angular iframe angular-pipe

我已阅读this postthis article。我相信我已经完成了所有建议:将管道添加到共享的模块中。

但是,无论我做什么,我都无法获取模板来查找我创建的管道。我的应用程序已经有一个共享模块,其他模块导入,所以我创建管道并将其添加到共享模块:

我用ng g pipe /shared/pipes/safe --flat --module shared --spec=false

创建了它

在SharedModule中,我还将其添加到declarationsproviders

一切都在运行,但我尝试使用管道,如:

<iframe width="600" height="360" [src]="video.acf.youtube_url | safe: 'url'" frameborder="0" allowfullscreen></iframe>

我刚收到错误

Error: Uncaught (in promise): Error: Template parse errors: The pipe 'safe' could not be found

管道本身就是

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl } from '@angular/platform-browser';

@Pipe({
  name: 'safe'
})
export class SafePipe implements PipeTransform {

  constructor(protected sanitizer: DomSanitizer) { }

  public transform(value: any, type: string): SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl {
    switch (type) {
      case 'html': return this.sanitizer.bypassSecurityTrustHtml(value);
      case 'style': return this.sanitizer.bypassSecurityTrustStyle(value);
      case 'script': return this.sanitizer.bypassSecurityTrustScript(value);
      case 'url': return this.sanitizer.bypassSecurityTrustUrl(value);
      case 'resourceUrl': return this.sanitizer.bypassSecurityTrustResourceUrl(value);
      default: throw new Error(`Invalid safe type specified: ${type}`);
    }
  }
}

1 个答案:

答案 0 :(得分:7)

您还需要在 SharedModule 导出下添加

exports: [
   SafePipe
]
相关问题