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

Ste*_*eve 2 javascript iframe angular-pipe angular

我已经阅读了这篇文章这篇文章.我相信我已经完成了所有建议:将管道添加到共享的模块中.

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

我用它创建了它 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}`);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Saj*_*ran 8

您还需要在SharedModule 导出下添加它,

exports: [
   SafePipe
]
Run Code Online (Sandbox Code Playgroud)