如何解决在 angular 11 中找不到错误管道
How to resolve error pipe could not be found in angular 11
我不是第一次使用管道,但现在我正在尝试使用我在带有 PipesModule
的单独文件夹“管道”中创建的安全管道,如下所示
这是管道 class:
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>
但仍然报错
Error: Uncaught (in promise): Error: NG0302: The pipe 'safe' could not be found!
Please can anyone help??
我有类似的问题,我的解决方案是简单地重新启动服务器。
如果上述方法不起作用,请关闭 IDE 并重新启动项目。缓存文件可能有问题
如果这不起作用,请清除浏览器缓存并重新加载您的应用程序
来自下面Demo on Stackblitz您的方法是正确的并且应该有效
我不是第一次使用管道,但现在我正在尝试使用我在带有 PipesModule
的单独文件夹“管道”中创建的安全管道,如下所示
这是管道 class:
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>
但仍然报错
Error: Uncaught (in promise): Error: NG0302: The pipe 'safe' could not be found! Please can anyone help??
我有类似的问题,我的解决方案是简单地重新启动服务器。
如果上述方法不起作用,请关闭 IDE 并重新启动项目。缓存文件可能有问题
如果这不起作用,请清除浏览器缓存并重新加载您的应用程序
来自下面Demo on Stackblitz您的方法是正确的并且应该有效