【问题标题】:ionic build --prod error: The pipe 'safe2' could not be foundionic build --prod 错误:找不到管道“safe2”
【发布时间】:2020-09-07 02:05:06
【问题描述】:

当我使用ionic serve 时,一切正常。

我有一个 viewReport 页面,它打开一个名为 viewModal 的模式页面。

我创建了一个管道,当我在 viewModal.module.ts 中调用该管道时,它给出了错误。 Stack Overflow 有人指导我将它添加到 viewReport.module.ts 页面中。我做到了,它奏效了。

但是现在当我使用ionic build --prod 进行生产构建时,它给出了错误:

ERROR in The pipe 'safe2' could not be found ("
    <ion-text>
      {{daily_report_desc }}
      <span [innerHtml]="[ERROR ->]daily_report_desc | safe2: 'html'">{{daily_report_desc}}</span>
      </ion-text>
    <ion-text>
")

[ERROR] An error occurred while running subprocess ng.

...

viewReport.module.ts的代码

import { Safe2Pipe } from './../pipes/safepipe2/safe2.pipe';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    ViewdailyreportPageRoutingModule
  ],
  declarations: [ViewdailyreportPage,ShortenPipe,ViewdreportmodalPage,SafePipe,Safe2Pipe],
  entryComponents: [ViewdreportmodalPage],
  exports:[Safe2Pipe]
})
export class ViewdailyreportPageModule {}
...

viewModal.page.html的代码

<ion-text>
  {{daily_report_desc }}
  <span [innerHtml]="daily_report_desc | safe2: 'html'">{{daily_report_desc}}</span>
</ion-text>

ionic serve 一切正常,但ionic build --prod 出错。

编辑 1

在 app.module.ts 中添加了 safe2pipe,见下图,现在当我打开 viewReport 页面(打开 viewModal 页面)时,它给出了错误 -

"): ng:///ViewReportPageModule/ViewModalPage.html@18:25
The pipe 'safe2' could not be found ("
    <ion-text>
      {{daily_report_desc }}
      <span [innerHtml]="[ERROR ->]daily_report_desc | safe2: 'html'">{{daily_report_desc}}</span>
      </ion-text>

编辑 2

safe2pipe.ts的代码

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl } from '@angular/platform-browser';

@Pipe({
  name: 'safe2'
})
export class Safe2Pipe 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}`);
        }
  }
}

【问题讨论】:

  • 你为什么使用 [innerHtml] 输入来应用你的管道?
  • 因为它的所见即所得编辑器,可以看到带有html属性的文本
  • 你的管道有模块吗?
  • 不,我有 safe2.pipe.tssafe2.spec.pipe.ts
  • 试试这个stackblitz.com/edit/…如果它有效我会给出答案。

标签: angular ionic-framework ionic2 ionic3 ionic4


【解决方案1】:

这里是堆栈闪电战: https://stackblitz.com/edit/ionic-c7zfbm

步骤如下:

  • 创建模态(带模​​块)
  • 模态模块上的导入管道
  • 在 app.module 上导入模态模块
  • 将模态页面放在app.module的entryComponents上。
  • 在模态html上调用管道

请注意,用于 ionic 的 stackblitz 已过时,为了工作,我做了一些变通方法。要查看内容,请检查并查找 html 代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-05
    • 2019-04-19
    • 2023-03-31
    • 1970-01-01
    • 2017-09-04
    • 2019-05-31
    • 2019-06-27
    • 1970-01-01
    相关资源
    最近更新 更多