vue內(nèi)嵌iframe跨域通信的實(shí)例代碼
vue內(nèi)嵌iframe跨域通信
1、Vue組件中如何引入iframe?
<template> <div class="act-form"> <iframe :src="src"></iframe> </div> </template> <script> export default { data () { return { src: '你的src' } } } </script>
如上,直接通過添加iframe標(biāo)簽,src屬性綁定data中的src,第一步引入就完成了
2、vue如何獲取iframe對(duì)象以及iframe內(nèi)的window對(duì)象?
在vue中,dom操作比不上jquery的$('#id')來的方便,但是也有辦法,就是通過ref
<template> <div class="act-form"> <iframe :src="src" ref="iframe"></iframe> </div> </template> <script> export default { data () { return { src: '你的src' } }, mounted () { // 這里就拿到了iframe的對(duì)象 console.log(this.$refs.iframe) } } </script>
然后就是獲取iframe的window對(duì)象,因?yàn)橹挥心玫竭@個(gè)對(duì)象才能向iframe中傳東西
<template> <div class="act-form"> <iframe :src="src" ref="iframe"></iframe> </div> </template> <script> export default { data () { return { src: '你的src' } }, mounted () { // 這里就拿到了iframe的對(duì)象 console.log(this.$refs.iframe) // 這里就拿到了iframe的window對(duì)象 console.log(this.$refs.iframe.contentWindow) } } </script>
3、vue如何向iframe內(nèi)傳送信息?
通過postMessage,具體關(guān)于postMessage是什么,自己去google, 我的理解postMessage是有點(diǎn)類似于UDP協(xié)議,就像短信,是異步的,你發(fā)信息過去,但是沒有返回值的,只能內(nèi)部處理完成以后再通過postMessage向外部發(fā)送一個(gè)消息,外部監(jiān)聽message 為了讓postMessage像TCP,為了體驗(yàn)像同步的和實(shí)現(xiàn)多通信互不干擾,特別制定的message結(jié)構(gòu)如下
{ cmd: '命令', params: { '鍵1': '值1', '鍵2': '值2' } }
通過cmd來區(qū)別這條message的目的
具體代碼如下
<template> <div class="act-form"> <iframe :src="src" ref="iframe"></iframe> <div @click="sendMessage">向iframe發(fā)送信息</div> </div> </template> <script> export default { data () { return { src: '你的src', iframeWin: {} } }, methods: { sendMessage () { // 外部vue向iframe內(nèi)部傳數(shù)據(jù) this.iframeWin.postMessage({ cmd: 'getFormJson', params: {} }, '*') }, }, mounted () { // 在外部vue的window上添加postMessage的監(jiān)聽,并且綁定處理函數(shù)handleMessage window.addEventListener('message', this.handleMessage) this.iframeWin = this.$refs.iframe.contentWindow }, handleMessage (event) { // 根據(jù)上面制定的結(jié)構(gòu)來解析iframe內(nèi)部發(fā)回來的數(shù)據(jù) const data = event.data switch (data.cmd) { case 'returnFormJson': // 業(yè)務(wù)邏輯 break case 'returnHeight': // 業(yè)務(wù)邏輯 break } } } </script>
4、iframe內(nèi)如何向外部vue發(fā)送信息?
現(xiàn)在通過點(diǎn)擊“向iframe發(fā)送信息”這個(gè)按鈕,從外部vue中已經(jīng)向iframe中發(fā)送了一條信息
{ cmd: 'getFormJson', params: {} }
那么iframe內(nèi)部如何處理這個(gè)信息呢?
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>iframe Window</title> <style> body { background-color: #D53C2F; color: white; } </style> </head> <body> <h1>Hello there, i'm an iframe</h1> <script> // 向父vue頁面發(fā)送信息 window.parent.postMessage({ cmd: 'returnHeight', params: { success: true, data: document.body.scrollHeight + 'px' } }, '*'); // 接受父頁面發(fā)來的信息 window.addEventListener("message", function(event){ var data = event.data; switch (data.cmd) { case 'getFormJson': // 處理業(yè)務(wù)邏輯 break; } }); </script> </body> </html>
到此這篇關(guān)于vue內(nèi)嵌iframe跨域通信的文章就介紹到這了,更多相關(guān)vue內(nèi)嵌iframe跨域通信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue使用pdf-lib實(shí)現(xiàn)為文件流添加水印并預(yù)覽
這篇文章主要為大家詳細(xì)介紹了Vue如何使用pdf-lib實(shí)現(xiàn)為文件流添加水印并預(yù)覽的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-03-03vue使用keep-alive實(shí)現(xiàn)數(shù)據(jù)緩存不刷新
這篇文章主要介紹了vue使用keep-alive實(shí)現(xiàn)數(shù)據(jù)緩存不刷新,這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10vue3使用vue-router及路由權(quán)限攔截方式
這篇文章主要介紹了vue3使用vue-router及路由權(quán)限攔截方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04Element 的 el-table 表格實(shí)現(xiàn)單元格合并功能
這篇文章主要介紹了Element 的 el-table 表格實(shí)現(xiàn)單元格合并功能,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-07-07vant開發(fā)微信小程序安裝以及簡(jiǎn)單使用教程
這篇文章主要介紹了vant開發(fā)微信小程序安裝以及簡(jiǎn)單使用教程,需要的朋友可以參考下2022-12-12Vue后臺(tái)實(shí)現(xiàn)點(diǎn)擊圖片放大功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Vue實(shí)現(xiàn)點(diǎn)擊圖片放大功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-12-12vue實(shí)現(xiàn)登錄數(shù)據(jù)的持久化的使用示例
在Vue.js中,實(shí)現(xiàn)登錄數(shù)據(jù)的持久化需要使用瀏覽器提供的本地存儲(chǔ)功能,Vue.js支持使用localStorage和sessionStorage來實(shí)現(xiàn)本地存儲(chǔ),本文就來介紹一下如何實(shí)現(xiàn),感興趣的可以了解一下2023-10-10