vue嵌套iframe一系列問題


最近在項目中遇到一個需求需要在一個項目中直接引用另一個項目,嘗試各種情況無果后選擇了iframe。現將調用過程中遇到的問題做一個分享。

router.go()的使用

  此情況主要適用於更改iframe中src值以后導致的路由跳轉混亂。

  詳細描述:當多次更改iframe->src屬性后,調用router.go(-1),不能實現路由后退上一級,而是將iframe當作一個窗口文檔,調用了該窗口文檔的window.history.go(-1),並未更改父級項目的路由后退功能。

  解決辦法:

  不通過改變iframe->src屬性值去訪問具體內容,采用window.location.replace(url)更改iframe將訪問的內容,具體代碼如下:

  1.  
    <!-- A.html -->
  2.  
    <template>
  3.  
    <iframe ref="iframe" scrolling="auto" width="100%" height="100%" frameborder="0" ></iframe>
  4.  
    </template>
  5.  
    <script>
  6.  
    export default {
  7.  
    name: 'ComponentsName',
  8.  
    data() {
  9.  
    return {
  10.  
    url: ''
  11.  
    }
  12.  
    },
  13.  
    watch: {
  14.  
    url(val) {
  15.  
    if (val) {
  16.  
    this.$refs.iframe.contentWindow.location.replace(val)
  17.  
    }
  18.  
    }
  19.  
    }
  20.  
    }
  21.  
    </script>
  22.  
    復制代碼

通信(父頁面和子頁面相互通信)

  兩個項目之間相互通信,涉及到跨域問題,子頁面不能直接調用父頁面的方法,父頁面同樣不能調用子頁面的方法。

  錯誤詳情:Error in created hook: "SecurityError: Blocked a frame with origin "http://*" from accessing a cross-origin frame."

  解決辦法: postMessage

  window.postMessage() 方法可以安全地實現跨源通信。該方法被調用時,會在所有頁面腳本執行完畢之后向目標窗口派發一個MessageEvent消息。代碼如下:

  1.  
    <!-- index.html -->
  2.  
    <!DOCTYPE html>
  3.  
    <html>
  4.  
    <head>
  5.  
    <title>Post Message</title>
  6.  
    </head>
  7.  
    <body>
  8.  
    <div>
  9.  
    <div id="color">Frame Color</div>
  10.  
    </div>
  11.  
    <div>
  12.  
    <iframe id="child" width="50%" src="http://172.16.110.188/test.html" height="50vw" scrolling="auto" frameborder="0"></iframe>
  13.  
    </div>
  14.  
    <script type="text/javascript">
  15.  
    window.οnlοad=function(){
  16.  
    document.getElementById('child').contentWindow.postMessage('getcolor','http://172.16.110.188');
  17.  
    }
  18.  
    window.addEventListener('message',function(e){
  19.  
    var color=e.data;
  20.  
    document.getElementById('color').style.backgroundColor=color;
  21.  
    }, false);
  22.  
    </script>
  23.  
    </body>
  24.  
    </html>
  25.  
    復制代碼
  1.  
    <!-- test.html -->
  2.  
    <!doctype html>
  3.  
    <html>
  4.  
    <head>
  5.  
    <style type="text/css">
  6.  
    html,body{
  7.  
    height:100%;
  8.  
    margin:0px;
  9.  
    }
  10.  
    #container{
  11.  
    widht:100%;
  12.  
    height:100%;
  13.  
    background-color:rgb(204, 102, 0);
  14.  
    }
  15.  
    </style>
  16.  
    </head>
  17.  
    <body style="height:100%;">
  18.  
    <div id="container" onclick="changeColor();">
  19.  
    click to change color
  20.  
    </div>
  21.  
    <script type="text/javascript">
  22.  
    var container=document.getElementById('container');
  23.  
    window.addEventListener('message',function(e){
  24.  
    if(e.source!=window.parent) return;
  25.  
    var color=container.style.backgroundColor;
  26.  
    window.parent.postMessage(color,'*');
  27.  
    }, false);
  28.  
    function changeColor () {
  29.  
    var color=container.style.backgroundColor;
  30.  
    if(color=='rgb(204, 102, 0)'){
  31.  
    color= 'rgb(204, 204, 0)';
  32.  
    } else{
  33.  
    color= 'rgb(204,102,0)';
  34.  
    }
  35.  
    container.style.backgroundColor=color;
  36.  
    window.parent.postMessage(color,'*');
  37.  
    }
  38.  
    </script>
  39.  
    </body>
  40.  
    </html>
  41.  
    復制代碼

  上面的例子實現了兩個不同域的頁面之間的通信。但由於我們此處用的是動態更改iframe.contentWindow.location來訪問的內容,如果此處父頁面要向子頁面發起通信需要在iframe中頁面加載完畢以后,不然子頁面無法獲取到通信數據。

應用場景

  子頁面需要調用父頁面的方法或則使用父頁面的數據時候,我們可以在子頁面向父頁面發起通信,讓父頁面調用該方法,或讓父頁面將數據傳輸過來。

注意事項

  postMessage支持對象傳遞,但不是所有瀏覽器都支持對象傳遞,在使用中還是使用字符串傳值更好。

轉載於:https://juejin.im/post/5cdac2bbf265da03925804c7

https://blog.csdn.net/weixin_34033624/article/details/91446152


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM