当前位置: 首页 > news >正文

iframe 实现跨域,两页面之间的通信

一、 背景

一个项目为vue2,一个项目为vue3,两个不同的项目实现iframe嵌入,并实现通信

二、方案

iframe跨域时,iframe组件之间常用的通信,主要是H5的possmessage方法

三、案例代码

父页面-vue2(端口号为127.0.0.1:8080)

<template><div><div class="container"><iframeref="iframeId"id="iframeId"src="http://127.0.0.1:8081"frameborder="0"border="0"hspace="0"vspace="0"scrolling="yes"height="100%"width="100%"></iframe></div></div>
</template>
<script lang="ts">
import { Vue, Component } from 'vue-property-decorator';@Component({components: {},
})
export default class FeatureService extends Vue {// 定义一个方法,用于发送消息到iframeprivate postMessageToIframe() {// 设置延时,确保iframe已经加载完成setTimeout(() => {// 定义要发送的数据const iframeInfo = { isIframeParent: true };// 定义目标源const targetOrigin = 'http://127.0.0.1:8081';// 获取iframe引用const iframe = this.$refs.iframeId;// 向iframe发送消息iframe.contentWindow.postMessage(JSON.stringify(iframeInfo),targetOrigin);}, 500);}private mounted() {// 在组件挂载后,发送消息到iframethis.postMessageToIframe();window.addEventListener('message', this.handleMessage, false);}private handleMessage(event: MessageEvent) {// 通过origin对消息进行过滤,避免遭到XSS攻击if (event.origin === 'http://127.0.0.1:8081') {console.log('子页面传输过来参数', event.data);}}
}
</script>
<style lang="less" scoped>
.container {width: 100%;margin-top: -40px;height: 100vh;overflow: hidden;#iframeId {width: 100%;display: block;}
}
</style>

子页面-vue3, (端口号为127.0.0.1:8081)

<template><div><!-- 顶部导航 --><TopMenu v-if="!iframeParentInfo.isIframeParent" /><div class="demo-sidebar-container demo-full-width"><!-- 侧边栏 --><SiderBar v-if="!iframeParentInfo.isIframeParent" /><!-- 内容容器 --><ContentBox /></div></div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'const iframeParentInfo = ref({})const messageHandler = (e) => {// 通过origin对消息进行过滤,避免遭到XSS攻击if (e.origin !== 'http://127.0.0.1:8080') returnif (typeof e.data === 'string') {parseData(e.data)}
}const parseData = (data) => {try {iframeParentInfo.value = JSON.parse(data)console.log('父页面传输过来参数', data)} catch (error) {console.error('解析JSON出错', error)iframeParentInfo.value = {}}
}onMounted(() => {// 获取 父向 子(iframe) 传递的信息window.addEventListener('message', messageHandler)// 子(iframe)向父传递信息window.parent.postMessage('Hello Parent!', '*')
})
</script>

四、案例效果

在这里插入图片描述

http://www.lryc.cn/news/164112.html

相关文章:

  • DevOps到底是什么意思?
  • 03JVM_类加载
  • Mysql如何对null进行排序(mysql中null排序)
  • 【基础计算机网络1】认识计算机网络体系结构,了解计算机网络的大致模型(下)
  • vscode 画流程图
  • uniapp-一些实用的api接口
  • 合宙Air724UG LuatOS-Air LVGL API控件-表格(Table)
  • 前缀和思想
  • Llama2-Chinese项目:1-项目介绍和模型推理
  • 论文于祥读及复现——《VDO-SLAM: A Visual Dynamic Object-aware SLAM System》
  • nuxt3项目使用pdfjs-dist预览pdf
  • mybatis-generator-maven-plugin使用
  • 基于SpringBoot开发的停车位管理系统(调用百度地图api)
  • STC8单片机PWM定时器+EC11编码器实现计数
  • MediaBox助力企业一站式获取音视频能力
  • 仅做笔记用:Stable Diffusion 通过 ControlNet 扩展图片 / 扩图
  • 代码随想录算法训练营19期第49天
  • 用shell脚本实现一个对数组求和的函数,数组通过实参传递给函数,写一个函数,输出当前用户的uid和gid,并使用变量接收结果
  • 运算符,switch
  • 运行java命令出现 Error: Invalid or corrupt jarfile XXX.jar
  • 在找工作时的准备工作:结合现状,针对意向企业做好充分准备
  • 微服务·数据一致-事务与分布式事务
  • GO语言篇之CGO
  • LVS负载均衡群集(NAT模式、IP隧道模式、DR模式)
  • PCL 使用克拉默法则进行三点定圆(二维)
  • MCAL实战二(S32K324-NXP EB tresos GPT驱动配置详解)
  • Python 图形化界面基础篇:什么是 Tkinter 以及为什么选择它
  • Learning From Data 中英文对照 1.THE LEARNING PROBLEM (第7页)
  • 自己编译静态ffmpeg freetype2 not found问题解决
  • 基于 Python 的音乐流派分类