我不是第一次使用管道,但现在我正在尝试使用我在一个单独的文件夹中创建的安全管道"管道";PipesModule
如下
这是管道类:
安全管道
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:找不到管道"safe"!有人能帮忙吗??
我也有类似的问题,我的解决方案只是重新启动服务器。
如果以上操作不起作用,请关闭IDE并重新启动项目。这可能是缓存文件的问题
如果这不起作用,也清除浏览器缓存并重新加载你的应用
从下面的Stacklitz演示您的方法是正确的,应该可以使用