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

企业级 Vue3 项目 iframe 封装方案

📦 企业级 Vue3 项目 iframe 封装方案

一、封装统一的 Iframe 组件

文件:src/components/IframePage.vue

<template><div class="iframe-container"><iframe:src="computedSrc"frameborder="0"width="100%":height="height":sandbox="sandbox"@load="onLoad"ref="iframeRef"></iframe></div>
</template><script setup>
import { ref, computed, watch } from 'vue'// 接收父组件传递的参数
const props = defineProps({src: { type: String, required: true },  // iframe地址height: { type: String, default: '100%' },  // iframe高度token: { type: String, default: '' },  // 鉴权token(如需要)sandbox: { type: String, default: 'allow-scripts allow-same-origin allow-forms allow-popups' }
})const iframeRef = ref(null)// 自动拼接 token,防止每次手动处理
const computedSrc = computed(() => {if (!props.token) return props.srcconst hasQuery = props.src.includes('?') ? '&' : '?'return `${props.src}${hasQuery}token=${props.token}`
})// 监听 iframe 加载事件
const onLoad = () => {console.log('iframe 加载完成:', computedSrc.value)// 你可以在这里做日志记录、上报监控、动态高度调整等
}
</script><style scoped>
.iframe-container {width: 100%;height: 100%;position: relative;
}
iframe {display: block;border: none;
}
</style>

二、配合 Vue Router 动态路由使用

举例场景:部分菜单为嵌入第三方系统

// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/Layout.vue'const routes = [{path: '/',component: Layout,children: [{ path: '', name: 'Home', component: () => import('@/views/Home.vue') },// 嵌入 iframe 菜单{path: '/bi-report',name: 'BiReport',meta: { title: 'BI报表系统' },component: () => import('@/views/IframeWrapper.vue'),props: { src: 'https://third-party.bi.com/report' }}]}
]export default createRouter({history: createWebHistory(),routes
})

三、封装通用 iframe 容器页

文件:src/views/IframeWrapper.vue

<template><div class="page-wrapper"><IframePage :src="src" height="100vh" /></div>
</template><script setup>
import { useRoute } from 'vue-router'
import IframePage from '@/components/IframePage.vue'// 允许通过路由传参
const route = useRoute()
const src = route?.params?.src || route?.props?.src || ''
</script><style scoped>
.page-wrapper {padding: 16px;background: #fff;height: 100%;
}
</style>

四、配合权限控制

假设你的权限控制已经封装好,使用时:

<IframePagev-if="hasPermission('report:view')"src="https://third-party.bi.com/report"height="100vh"
/>

五、进阶:支持全局动态嵌入 iframe

如果未来你做微前端 or SaaS 多系统整合,可以考虑 iframe 动态管理:

  • 配置表管理 iframe 菜单
  • 后端动态下发 URL
  • 支持多系统登录态隔离、传参

例如:

[{"name": "ERP系统","path": "/erp","src": "https://erp.xxx.com"},{"name": "报表中心","path": "/report","src": "https://bi.xxx.com"}
]

前端路由动态生成 iframe 页面。


✅ 核心优势总结

  • ✅ 统一 iframe 组件封装
  • ✅ 兼容权限控制
  • ✅ 支持 Token 拼接、沙箱隔离
  • ✅ 动态路由可复用
  • ✅ 企业项目通用架构

相关文章:

  • Excel单元格数值统计 - 华为OD机试真题(Python题解)
  • 宇宙尽头是WPS之——【Excel】一个自动重新排序的宏
  • Vivaldi浏览器6.4.3160.42安装教程 - 64位下载安装步骤详解(包含历史版本)
  • “贴身日记”购物网站的设计与实现
  • Matlab自学笔记五十九:符号变量的代入和替代subs精讲
  • Flutter中将bytes转换成XFile对象上传
  • 8.TCP Server端实现
  • AWS ELB 可观测性最佳实践
  • 34. 在排序数组中查找元素的第一个和最后一个位置
  • 力扣刷题——长度最小的子数组
  • 在 Qt 开发中,.toLocal8Bit().constData() 和 .toUtf8()有什么区别
  • 【React源码解析】初识JSX
  • You Only Look Once Unified, Real-Time Object Detection论文笔记
  • django ReturnDict 如何修改内容
  • C++(从C到C++)
  • Java系统开发:前端与后端的协同开发实践
  • 老项目重构难题破解:飞算 JavaAI 如何实现技术升级突围
  • 数字ic后端设计从入门到精通7(含fusion compiler, tcl教学)sta时序分析
  • Java基础学习-多线程
  • python+uniapp基于微信小程序健康管理系统
  • 免费注册网站怎么做链接/百度人工服务热线电话
  • 免费产品网站建设/如何进行品牌营销
  • 深圳市宝安区建设局网站/做营销怎样才能吸引客户
  • wordpress 打赏 手机/seo实战视频
  • 做企业网站都有什么平台/免费seo网站诊断免费
  • 网站在工信部备案如何做/搜狗竞价推广效果怎么样