遇到一个问题:iframe加载一个html一直报错,Error: Eequired a safe ResourceURL,got a HTML
解决办法:引入“DomSanitizer”
具体实现:
import { DomSanitizer } from "@angular/platform-browser";
constructor(private sanitizer: DomSanitizer,) {
this.fileUrl= this.sanitizer.bypassSecurityTrustResourceUrl(res.data.link);
//注意是bypassSecurityTrustResourceUrl
//有些资料上写的是bypassSecurityTrustHtml,这个方法会报错
}
HTML:
<iframe *ngIf="this.articleType == 'WD'||this.articleType == 'LJ'"
height=100%
width=100%
[src]="fileUrl"></iframe>