angular - 意外管道'EscapeHtmlPipe 在escape-html.pipe.ts 请添加@NgModule 注释
问题描述
运行ng build --prod
给我以下错误。
错误:由模块“appModule in app.module.ts”导入的意外管道“escape-html.pipe.ts 中的 EscapeHtmlPipe”。请添加 @NgModule 注释。
工作正常ng serve
。问题ng build --prod
只有
文件中的代码如下
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import{ EscapeHtmlPipe } from '../../Shared/pipes/escape-html.pipe';
@NgModule({
imports: [CommonModule],
declarations: [EscapeHtmlPipe],
exports: [EscapeHtmlPipe]
})
export class EscapeHtmlModule { }
管道的代码文件如下
import { Pipe, PipeTransform } from '@angular/core';
import { SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl, DomSanitizer } from '@angular/platform-browser';
@Pipe({
name: 'escapeHtml'
})
export class EscapeHtmlPipe implements PipeTransform {
constructor(protected sanitizer: DomSanitizer) { }
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}`);
}
}
}
在 AppModule 我已经这样做了
import { EscapeHtmlModule } from './Module/escape-html/escape-html.module';
NgModule 部分如下所示
NgModule({
declarations: [
AppComponent,
// EscapeHtmlPipe,
NavMenuComponent,
OpportunityComponent,
HomeComponent,
FileUploadComponent,
TinymceComponent,
SummaryComponent,
OpportunityListComponent
],
imports: [
BrowserModule,
MaterialModule,
FormsModule,
ReactiveFormsModule,
AppRoutingModule,
BrowserAnimationsModule,
HttpClientModule,
DragDropModule,
ScrollDispatchModule,
EditorModule,
// EscapeHtmlPipe,
DragulaModule.forRoot(),
ContextMenuModule.forRoot(), EscapeHtmlModule
],
providers: [
RFPDocumentService,
ColorChangeService,
CategoryService,
InsertCategoryAttributeService
],
bootstrap: [AppComponent],
entryComponents:[TinymceComponent,OpportunityListComponent]
})
但是在我在中间件中添加 EscapeHtmlModule 之前,问题甚至存在,因为之前我直接在 AppModule 中添加了管道,这会产生错误。任何帮助将不胜感激谢谢
解决方案
您的错误消息:ERROR in : Unexpected pipe 'EscapeHtmlPipe in escape-html.pipe.ts' imported by the module 'AppModule in app.module.ts'. Please add a @NgModule annotation
表示您正在EscapeHtmlPipe
将AppModule
. 您需要将 导入EscapeHtmlModule
到您的AppModule
not 中EscapeHtmlPipe
。
Pipes
以下是使用 shared (或Components
and )创建共享模块时的基本流程示例Directives
:
- 创建一个新的
NgModule
(你称之为 EscapeHtmlModule) - 创建
Pipe
并将其添加到 EscapeHtmlModuledeclarations
和exports
数组 - 将管道需要工作的任何其他模块添加到
imports
数组 - 转到您的
AppModule
并导入EscapeHtmlModule
(将其添加到imports
数组中)
推荐阅读
- database - 在哪里可以找到我尝试使用 Hibernate 访问的数据库?
- python - 启动 Juypter Lab 的问题
- python - LabelEncoding 多个分类列
- python - 在 tkinter 文本小部件上显示 2d 列表
- python - Docker for django 开发自动更新
- c# - 有没有办法使用 aws 服务 api 从外部删除 aws EC2 实例 Windows 服务器文件夹和日志文件
- swift - SceneKit Swift - 使用文件中的 HDR 环境光照贴图
- android - 如何通过从 /asset/location 读取 Json 文件来制作动态页面
- django - 我看不懂这段代码也不知道怎么用?
- excel - 有基于公式中的公式的区域参考