管道未找到:Angular 5自定义管道

3

我已经阅读了这篇文章这篇文章。我相信我已经按照建议做了一切:将该管道添加到共享模块中。

然而,不管我怎么做,我的模板都无法找到我创建的管道。我的应用程序已经有了一个共享模块,其他模块导入它,所以我在共享模块中创建了管道并将其添加到其中:

我使用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个回答

12

您需要将其添加到 SharedModule 的 exports 中,

exports: [
   SafePipe
]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接