是的,并非所有东西都能在 OOTB 中正常工作。
如您所述,您可以自己解决此问题,将默认设置替换为CMSParagraphComponent
自定义问题:
export const paragraphCmsConfig: CmsConfig = {
cmsComponents: {
CMSParagraphComponent: {
component: YourParagraphComponent,
},
},
}
在YourParagraphComponent
你的内心扩展给定的ParagraphComponent
@Component({
selector: 'your-paragraph',
templateUrl: './your-paragraph.component.html',
styleUrls: ['./your-paragraph.component.scss'],
})
export class YourParagraphComponent extends ParagraphComponent {}
并实施你自己的SafeHtmlPipe
<div class="your-paragraph" *ngIf="component.data$ | async as data" [innerHTML]="data.content | trust: 'html'"></div>
在我们的例子中,管道被命名为trust
@Pipe({
name: 'trust',
})
export class YourSafeHtmlPipe 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}`)
}
}
}
将所有模块连接在一起并将其导入您的自定义实现
@NgModule({
imports: [CommonModule, CmsParagraphModule],
declarations: [YourParagraphComponent],
exports: [YourParagraphComponent],
providers: [provideConfig(paragraphCmsConfig),YourSafeHtmlPipe],
})
export class YourParagraphComponentModule {}