【问题标题】:How to resolve error pipe could not be found in angular 11如何解决在角度 11 中找不到错误管道
【发布时间】:2021-01-04 20:13:55
【问题描述】:

我不是第一次使用管道,但现在我正在尝试使用我在带有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:找不到“安全”管道! 请问有人可以帮忙吗??

【问题讨论】:

  • 尝试从 PipesModule 中移除 CommonModule
  • @feihoa 不,这对我没有帮助

标签: pipe angular11


【解决方案1】:

我有类似的问题,我的解决方案是简单地重新启动服务器

如果上述方法不起作用,请关闭 IDE 并重新启动项目。可能是缓存文件的问题

如果这不起作用,请清除浏览器缓存并重新加载您的应用

从下面Demo on Stackblitz你的方法是正确的,应该可以工作

【讨论】:

  • 是的,我做到了,现在可以了,谢谢
猜你喜欢
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 2020-07-23
  • 1970-01-01
  • 2014-09-24
  • 2012-03-02
相关资源
最近更新 更多