需求: 頁面中有一個網頁組件(由iframe編寫),此iframe顯示在一個輸入框中,當修改輸入框中地址的時候,要求改變網頁組件中的內容
網頁組件中的代碼(html的部分)
<iframe #Iframe [src]="testUrl" frameborder="0" width="100%" height="100%"> </iframe>
網頁組件中的代碼(ts的部分)
...省略 export class DesignWebInputComponent implements OnInit{ testUrl ; }
此時問題出現了,頁面無法顯示內容
不要慌,有辦法可以解決
constructor( private sanitizer:DomSanitizer) {}
導入DomSanitizer 這個類 并使用其中的bypassSecurityTrustResourceUrl() 轉換url的格式 如下
trustUrl(url: string) { if(url){ return this.sanitizer.bypassSecurityTrustResourceUrl(url); } }
html中
<iframe #Iframe [src]="trustUrl(testUrl)" frameborder="0" width="100%" height="100%"> </iframe>
在這里寫了個trustUrl()轉換 testUrl 這樣就可以顯示了
總結: 使用 DomSanitizer 類中的 bypassSecurityTrustResourceUrl() 來轉換url
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com