【vue嵌套iframe】实现项目重构


适用于使用vue重构及vue访问其他服务页面
基于vue3示例页面添加嵌套iframe的页面

创建 iframe 通用组件

IframeTemplate.vue

  1. 页面布局
        <template>
          <div class="iframe-container"></div>
        </template>
    
        <style scoped>
        .iframe-container {
          width: 50%;
          height: calc(100vh - 124px);
        }
        </style>
        ```
    
  2. iframe标签创建
        /**iframe 标签创建 */
        function createIframe() {
          try {
            const { meta: { isIframe }, path, query } = route
            if (!isIframe) return;
    
            // 地址拼接、带参query等
            const baseUrl = getBaseUrl() + path + (isIframe ? ".html" : "")
            let commonIframe = $("#common-iframe")
            if (commonIframe.length) {
              commonIframe.attr("src", baseUrl)
            } else {
              commonIframe = $('<iframe class="iframe-content" id="common-iframe" width="100%" height="100%" style="overflow:hidden;border:0 none; outline:0"></iframe>')
              $(".iframe-container").prepend(commonIframe)
    
              commonIframe.ready(() => {
                commonIframe.attr("src", baseUrl)
              })
            }
          } catch (err) {
            console.log(err)
          }
        }
        /** 根据环境区分iframe页面地址前缀 
         * server & base 可以根据环境 配置不同的值
         * 1.webpack添加区分环境的配置
         * 2.在对应的 .env文件中配置变量
         */
        function getBaseUrl() {
          const { VUE_WEB_IFRAME_SERVER: server, VUE_WEB_BASEURL: base } = process.env
          /*
           * 示例: 使用apache代理静态页面(http://localhost:8080/iframe-pages/update.html)
           * 此处的server=http://localhost:8080,base="",route="/iframe-pages/update"
           */
          return server + base
        }
        ```
    
  3. 监听路由,实现iframe地址更新,访问不同页面
      /* 监听路由变更 - iframe 页面地址更新 */
       watch(route, () => {
         createIframe()
       });
       onMounted(() => {
         createIframe()
       });
    

添加页面及路由

  1. 添加路由
         // router/index.js
         
         // iframe页面独立访问地址: http://localhost:8080/iframe-pages/update.html
         /** 添加iframe 页面路由 */
         const iframePages = [
           {
             path: '/iframe-pages/base',
             name: "base"
           },
           {
             path: '/iframe-pages/includes',
             name: "includes"
           },
           {
             path: '/iframe-pages/update',
             name: "update"
           }
         ]
         let iframeRoute = []
         iframePages.map(item => {
           const { path, name } = item
           iframeRoute.push(
             {
               path,
               name,
               meta: { isIframe: true },
               component: () => import('../views/IframePage.vue')
             }
           )
         })
    
         const router = createRouter({
           // 其他...
           routes: [
             // 其他...
             ...iframeRoute
           ]
         })
         ```
    
  2. iframe 通用页面
        <!-- IframePage.vue -->
        
        <template>
          <IframeTemplate></IframeTemplate>
        </template>
        <script setup>
        import IframeTemplate from "@components/IframeTemplate.vue";
        </script>
        <style scoped lang="scss"></style>
        ```
    
  3. iframe 页面快捷菜单
       <!-- App.vue --> 
       <RouterLink to="/iframe-pages/base">Base</RouterLink>
       <RouterLink to="/iframe-pages/includes">Includes</RouterLink>
       <RouterLink to="/iframe-pages/update">Update</RouterLink>
       ```
       
    

此时页面就可以根据路由访问嵌套的页面了.

进阶:vue 与 iframe 的双向通讯

实现vue 与 iframe 的双向通讯

代码下载

查看代码地址

相关推荐

  1. vue嵌套iframe实现项目重构

    2024-05-26 05:32:11       37 阅读
  2. iframe嵌入Vue页面实现免登方法

    2024-05-26 05:32:11       34 阅读
  3. vue项目获取 iframe 中的DOM元素

    2024-05-26 05:32:11       59 阅读
  4. 页面嵌入iframe Cookie丢失问题解决

    2024-05-26 05:32:11       56 阅读
  5. iframe标签嵌入 网页传值

    2024-05-26 05:32:11       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-26 05:32:11       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-26 05:32:11       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-26 05:32:11       82 阅读
  4. Python语言-面向对象

    2024-05-26 05:32:11       91 阅读

热门阅读

  1. glances的安装方式

    2024-05-26 05:32:11       30 阅读
  2. netstat命令检查端口是否监听

    2024-05-26 05:32:11       31 阅读
  3. C语言基础-如何避免内存泄漏

    2024-05-26 05:32:11       36 阅读
  4. 泛型中K T V E ? Object等分别代表的含义

    2024-05-26 05:32:11       30 阅读
  5. 所有笔记总结目录

    2024-05-26 05:32:11       28 阅读
  6. Python库之Scrapy的简介、安装、使用方法详细攻略

    2024-05-26 05:32:11       34 阅读
  7. 诺兰电影欣赏笔记

    2024-05-26 05:32:11       32 阅读
  8. Kafka之【消费消息】

    2024-05-26 05:32:11       28 阅读
  9. 解决uniApp 中不能直接使用 Axios 的问题

    2024-05-26 05:32:11       35 阅读
  10. 关系型数据库的三范式理解

    2024-05-26 05:32:11       38 阅读
  11. 汇编小程序

    2024-05-26 05:32:11       28 阅读
  12. 怎样使用类和对象

    2024-05-26 05:32:11       26 阅读
  13. CentOS配置应用服务自启动

    2024-05-26 05:32:11       28 阅读