AvueAvue
首页
  • 开发指南
  • Skill开发
  • 在线测试工具
  • Form组件
  • Crud组件
  • Default组件
  • Data组件
  • Component组件
产品
工作台
授权
联系
2.x文档
个人支付接口
首页
  • 开发指南
  • Skill开发
  • 在线测试工具
  • Form组件
  • Crud组件
  • Default组件
  • Data组件
  • Component组件
产品
工作台
授权
联系
2.x文档
个人支付接口
  • Article 文章
  • Group 分组容器
  • Title 标题文本
  • CountUp 数字动画
  • Comment 评论
  • Chat 客服聊天
  • Captcha 验证码
  • Contextmenu 右键菜单
  • Clipboard 复制文本
  • DialogForm 弹窗表单
  • Draggable 拖拽
  • Export Excel 导入与导出
  • Flow 流程
  • FilterBuilder 高级筛选
  • FileManager 文件管理
  • QRCode 二维码
  • Iframe 嵌入页面
  • ImagePreview 图片预览
  • Login 登录组件
  • License 授权书
  • Print 打印
  • Screenshot 页面截图
  • Search 标签搜索
  • Sign 电子签名
  • TextEllipsis 超出文本省略
  • Tabs 选项卡
  • Tree 树型
  • Video 摄像头
  • Calendar 日历组合示例

Iframe 嵌入页面

avue-iframe 为嵌入页面提供加载、超时、错误和空状态,并封装了刷新、聚焦与跨窗口消息通信能力。

效果预览可直接操作下方示例
正在加载示例…
<template>
  <el-space wrap :size="12">
    <el-button type="primary" @click="reload">重新加载</el-button>
    <el-button @click="sendMessage">通知子页面</el-button>
  </el-space>
  <div class="iframe-demo__content">
    <avue-iframe
      ref="iframeRef"
      src="/examples/iframe-child.html"
      :timeout="10000"
      @load="appendLog('页面加载完成')"
      @message="handleMessage"
      @timeout="appendLog('页面加载超时')"
    />
  </div>
  <div class="iframe-demo__log">{{ log }}</div>
</template>

<script setup>
import { ref } from 'vue';

const iframeRef = ref();
const log = ref('等待子页面消息…');

const appendLog = (message) => {
  log.value = `${new Date().toLocaleTimeString()} ${message}`;
};

const reload = () => {
  iframeRef.value?.reload();
};

const sendMessage = () => {
  iframeRef.value?.postMessage(
    { type: 'notice', content: '来自父页面的消息' },
    window.location.origin,
  );
};

const handleMessage = ({ data, origin }) => {
  if (origin !== window.location.origin) return;
  appendLog(`收到子页面消息:${JSON.stringify(data)}`);
};
</script>

<style scoped>
.iframe-demo__content {
  height: 240px;
  margin-top: 16px;
  border: 1px solid var(--el-border-color);
}

.iframe-demo__log {
  margin-top: 12px;
  color: var(--el-text-color-secondary);
  font-size: 13px;
}
</style>

Attributes

参数说明类型可选值默认值
src页面地址string-''
nameiframe 名称string--
titleiframe 标题string-嵌入页面
width容器宽度,数字会自动追加 pxstring / number-100%
height容器高度,数字会自动追加 pxstring / number-100%
showLoading是否显示加载态booleantrue / falsetrue
loadingText加载提示文本string-页面加载中...
timeout加载超时时间,单位 ms;0 表示不启用number-0
timeoutText超时提示文本string-页面加载超时
errorText加载失败提示文本string-页面加载失败

Events

事件名说明回调参数
before-load开始加载或刷新前触发{ src, iframe, window }
loadiframe 加载完成时触发{ src, iframe, window }
readyiframe 加载完成时触发,与 load 同时触发{ src, iframe, window }
erroriframe 加载失败时触发{ src, iframe, window, error }
timeout达到 timeout 后仍未加载完成时触发{ src, iframe, window, timeout }
reload调用 reload 后触发{ src, iframe, window }
message收到当前 iframe 窗口发出的消息时触发{ data, origin, source }

Slots

插槽名说明插槽参数
emptysrc 为空时的内容-
error加载失败或超时时的内容{ error, timedOut, reload }

Methods

通过组件 ref 调用。

方法名说明参数
reload重新创建并加载 iframe返回 boolean
focus聚焦 iframe 窗口返回 boolean
postMessage向 iframe 发送消息;无可用窗口时返回 false,成功调用后返回 truedata, targetOrigin = '*', transfer?
getIframe获取当前 iframe DOM;未挂载时为 null无
getWindow获取 iframe 的 contentWindow;未挂载时为 null无

通信安全

跨窗口通信时请传入明确的 targetOrigin,不要在生产环境使用默认的 '*'。在 message 回调中还应校验 origin 和消息数据结构,避免处理来自非受信页面的消息。

加载与布局说明

height="100%" 需要父容器具有明确高度,也可直接传 :height="300"。src 为空时显示空状态,reload() 返回 false。

before-load 在开始加载时触发,此时 DOM 或窗口可能尚未就绪;刷新期间也可能仍引用旧 iframe。需要操作新窗口时,在 load 或 ready 后取实例。ready 与原生加载事件同步,不代表子页面内的业务数据已经加载完成;业务就绪可由子页面通过消息主动通知。

服务端渲染项目请把启用超时的 iframe 放在客户端渲染边界内,例如 VuePress 的 ClientOnly。跨域页面受浏览器同源策略约束,组件不会绕过该限制,也不会把所有外部属性自动转发给内部 iframe。

最后更新:
贡献者: smallwei
Prev
QRCode 二维码
Next
ImagePreview 图片预览