遇到一個問題: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>