【发布时间】: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.ts和safe2.spec.pipe.ts -
试试这个stackblitz.com/edit/…如果它有效我会给出答案。
标签: angular ionic-framework ionic2 ionic3 ionic4