问题描述
我不是第一次使用管道,但现在我尝试使用我在单独的文件夹“管道”中创建的安全管道,其中 PipesModule
如下
这是管道类:
safe.pipe.ts
import { Pipe,PipeTransform } from '@angular/core';
import { DomSanitizer,SafeHtml,SafeResourceUrl,SafeScript,SafeStyle,SafeUrl } from '@angular/platform-browser';
@Pipe( {
name: 'safe'
} )
export class SafePipe implements PipeTransform {
constructor ( protected sanitizer: DomSanitizer ) { }
public transform ( url: any ) {
return this.sanitizer.bypassSecurityTrustResourceUrl( url );
}
}
这是模块文件pipes.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SafePipe } from './safe.pipe';
@NgModule( {
declarations: [
SafePipe
],exports: [
SafePipe
],imports: [
CommonModule
]
} )
export class PipesModule { }
并且我在要使用管道的组件模块中导入了 PipesModule,
@NgModule( {
declarations: [ GenerateContractComponent ],imports: [
CommonModule,FormsModule,NgxMaskModule,PipesModule
]
} )
然后在 HTML 中使用管道如下:
<iframe width="100%" style="height:-webkit-fill-available" [src]="pdfURL | safe" frameborder="0"></iframe>
但还是报错
错误:未捕获(承诺):错误:NG0302:找不到管道“安全”! 请问有人可以帮忙吗??
解决方法
我有类似的问题,我的解决方案是简单地重启服务器。
如果上述方法不起作用,请关闭IDE并重新启动项目。可能是缓存文件的问题
如果这不起作用,请清除浏览器缓存并重新加载您的应用
从下面 Demo on Stackblitz 你的方法是正确的,应该工作